美文网首页
RAC监听键盘

RAC监听键盘

作者: 李木的 | 来源:发表于2017-03-24 17:11 被阅读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

相关文章

  • RAC监听键盘

  • iOS 监听键盘事件

    Swift RAC 监听 系统方法监听 响应方法 remove observer Objective-C RAC 监听

  • RAC的常见应用场景

    这里写RAC常见的应用场景 RAC集合 代替KVO 监听事件 代替通知 监听文本框5.代理 RAC集合 RACTu...

  • ReactiveCocoa(RAC) 2018-01-25

    RAC监听按钮点击事件 [[self.button rac_signalForControlEvents:UICo...

  • RAC给UITextField添加代理回调

    使用RAC监听UITextField的文本可以用rac_textSignal,但是rac_textSignal是实...

  • ReactiveObjC使用

    一.RAC基础使用 监听方法调用 KVO 通知 监听View事件 监听Timer RACSignal RACSig...

  • RAC(ReactiveCocoa)小记-新人必看

    一、配置RAC环境 : 二、RAC能干什么? 1、对事件的监听 例如我们监听一个textfield输入完成的事件,...

  • 2.4 UITableView-聊天布局

    键盘处理 1.监听键盘通知// 监听键盘通知[[NSNotificationCenter defaultCente...

  • RAC使用

    RAC常用宏 KVO监听使用 RAC宏使用 信号类使用 使用信号模拟代理 rac_sequence遍历字典 解包元...

  • iOS开发监听键盘事件

    注册通知监听器,监听键盘弹起事件 注册通知监听器,监听键盘收起事件 键盘弹出调用该方法 键盘收起调用该方法 开始视...

网友评论

      本文标题:RAC监听键盘

      本文链接:https://www.haomeiwen.com/subject/wkpfottx.html