设置使用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监听键盘
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.bug描述: 一,系统键盘,当键盘弹起后,再执行另外的alert操作,键盘收起,界面Y值并不会复原;二,第三方...
- android:imeOptions="actionSend" // 起关键作用 键盘显示与隐藏 监听 // 使用:
- 转自链接 http://www.cnblogs.com/niupi/p/6251663.html 软键盘的隐藏方...