//通过设置该监听,用于获取键盘弹出时的轨迹属性及键盘frame
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//通过设置该监听,用于获取键盘关闭时的轨迹属性及键盘frame
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
#pragma mark - 键盘事件监听
- (void)keyboardWillShow:(NSNotification *)notice {
UIViewAnimationCurve _animationCurve;
CGFloat _animationDuration;
NSDictionary *userInfo = [notice userInfo];
CGRect endFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat _keyboardHeight = (endFrame.origin.y != kCCScreenHeight()) ? endFrame.size.height:0;
if (!_keyboardHeight) return;
CGRect beginRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
if(!(beginRect.size.height > 0 && ( fabs(beginRect.origin.y - endRect.origin.y) > 0))) return;
//动画执行时长
_animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
//运行轨迹属性
_animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
//这样就可以与键盘动画无缝衔接了
[UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:^{
// 修改frame
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)noti {
//获取键盘的高度
NSDictionary *userInfo = [noti userInfo];
// NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration delay:0 options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:^{
// 修改frame
} completion:nil];
}
iOS 键盘监听与视图滚动
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.先添加对键盘的监听 当系统消息出现UIKeyboardWillShowNotification和UIKeybo...
- 1.先添加对键盘的监听 当系统消息出现UIKeyboardWillShowNotification和UIKeybo...
- 最近项目中有一个聊天的页面,不是本人做的,其中键盘视图有一个自己的表情视图,这个选择表情的视图是一个滚动视图,如果...
- 由于键盘弹出的时候是用到了通知,所以我们先来看看通知的用法: 先创建两个类:LHLPerson和LHLZhaoPi...