RAC监听键盘

2017-03-24  本文已影响201人  李木的
设置使用RAC方法监听键盘的偏移量
- (void)addNotification{
    _textView.delegate = self;
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil]
      deliverOn:[RACScheduler mainThreadScheduler]]
     subscribeNext:^(NSNotification *value) {
         [UIView animateWithDuration:0.25 animations:^{
             self.inputToolBarBottomConstraint.constant = [value.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;;
//             self.myCollectionView.constant = 216;

         } completion:^(BOOL finished) {
             if (self.dataSource.count >0) {
                 [self fy_dispatch_async_mainQueue:^{
                     [self scrollToBottom];
                 }];
             }
         }];
     }];
    
    RACSignal *signal = [RACSignal merge:@[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil],
                                           [[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillHideNotification object:nil],
                                           ]];
    [[[signal
       takeUntil:[self rac_signalForSelector:@selector(viewWillDisappear:)]]
      deliverOn:[RACScheduler mainThreadScheduler]]
     subscribeNext:^(NSNotification *value) {
         [UIView animateWithDuration:0.25 animations:^{
             CGFloat height = [value.name isEqualToString:UIKeyboardWillHideNotification] ? 0 : [value.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
             self.inputToolBarBottomConstraint.constant = height;
         }];
     }];
}

//pragma mark - UITextView代理
-(void)textViewDidChange:(UITextView *)textView {
    // 1.计算textView的高度
    CGFloat textViewH = 0;
    CGFloat minHeight = 33 + 3; // textView最小的高度
    CGFloat maxHeight = 100 + 3 +10; // textView最大的高度
    // 获取contentSize 的高度
    CGFloat contentHeight = textView.contentSize.height;
    if (contentHeight < minHeight) {
        textViewH = minHeight;
        [textView setContentInset:UIEdgeInsetsZero];
    } else if (contentHeight > maxHeight) {
        textViewH = maxHeight + 4.5;
        [textView setContentInset:UIEdgeInsetsMake(-5, 0, -3.5, 0)];
    } else {
        if (contentHeight ==  minHeight) {
            [textView setContentInset:UIEdgeInsetsZero];
            textViewH = minHeight;
        } else {
            textViewH = contentHeight - 8;
            [textView setContentInset:UIEdgeInsetsMake(-4.5, 0, -4.5, 0)];
        }
    }
    // 3.调整整个InputToolBar 的高度
    self.height.constant = 6 + 7 + textViewH;
    CGFloat changeH = textViewH - self.previousTextViewContentHeight;
    if (changeH != 0) {
        // 加个动画
        [UIView animateWithDuration:0.25 animations:^{
            [self.view layoutIfNeeded];
            if (textView.text.length) {
            }
            // 4.记光标回到原位
            // 下面这几行代码需要写在[self.view layoutIfNeeded]后面,不然系统会自动调整为位置
            if (contentHeight < maxHeight) {
                [textView setContentOffset:CGPointZero animated:YES];
                [textView scrollRangeToVisible:textView.selectedRange];
            }
        }];
        self.previousTextViewContentHeight = textViewH;
    }
    if (contentHeight > maxHeight) {
        [UIView animateWithDuration:0.2 animations:^{
            if (self.contentOffsetY) {
                if (textView.selectedRange.location != textView.text.length && textView.contentOffset.y != self.contentOffsetY) return;
            }
            [textView setContentOffset:CGPointMake(0.0, textView.contentSize.height - textView.frame.size.height - 3.5)];
            self.contentOffsetY = textView.contentOffset.y;
        }];
        [textView scrollRangeToVisible:textView.selectedRange];
    }
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
上一篇下一篇

猜你喜欢

热点阅读