今天项目要求文章曾加评论功能,类似于微信朋友圈的评论,当点击评论时textfiled随着键盘一起出现,很流畅,研究了许久,终于完成了
注册通知
[XMNotificationCenter addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[XMNotificationCenter addObserver:self selector:@selector(keyboardDidHidden:) name:UIKeyboardWillHideNotification object:nil];
键盘出现
- (void)keyboardDidShow:(NSNotification *)notification
{
//获取键盘高度
keyboardH = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
// 获取键盘弹出动画时间
NSValue *animationDurationValue = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//改变TextFiled的Y
[animationDurationValue getValue:&animationDuration];
[UIView animateWithDuration:animationDuration animations:^{
_textview.y = H_PATH - keyboardH - 50;
}];
DEBUGLog(@"出现%f",keyboardH);
}
键盘消失
- (void)keyboardDidHidden:(NSNotification *)notification
{
DEBUGLog(@"消失");
//将TextFiled的Y至初始位置并隐藏
[UIView animateWithDuration:animationDuration animations:^{
_textview.y = H_PATH - 50;
_textview.hidden = YES;
}];
}
注销通知
- (void)dealloc
{
[XMNotificationCenter removeObserver:self];
}