监听键盘高度实现评论功能

来源:互联网 发布:国家人工智能战略 编辑:程序博客网 时间:2024/04/28 15:19

效果展示:


代码:

//监听键盘出现和消失
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

#pragma mark 键盘出现
-(void)keyboardWillShow:(NSNotification *)note
{
// 这样就拿到了键盘的位置大小信息frame,然后根据frame进行高度处理之类的信息
NSDictionary *info = note.userInfo;
CGRect beginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
id animationDurationValue = [[note userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
float time = [animationDurationValue floatValue];
[UIView animateWithDuration:time animations:^{
if (self.commentTF.isFirstResponder && /*解决多次调用的问题,只执行最后一次*/beginFrame.size.height>0 && (beginFrame.origin.y-endFrame.origin.y>0)) {
self.toolView.mj_y = kSH-64-50*kAdaptValue - endFrame.size.height;
}
}];

}
#pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification *)note
{
if (self.commentTF.isFirstResponder) {
self.toolView.mj_y = kSH-64-50*kAdaptValue;
// flagToolHeight = 1;
}
}

别忘了。。。
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


0 0