我有一个需求,需要在即将推出的项目中增加聊天功能。
与此同时,我一直在尝试实现我所期望的简单事情,即在屏幕底部添加一个UIView,其中包含一个UITextView。当用户点击UITextView时,它会随着键盘上移而动画效果显示。
我的实现方式已经能够正常工作,然而不幸的是,键盘的动画效果略慢于它上面的视图。以下是我的目前的实现方式:
注册键盘通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
键盘通知方法:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSInteger curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration delay:0 options:curve animations:^{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
_chatViewBottomConstraint.constant = keyboardFrame.size.height;
[self.view layoutIfNeeded];
} completion:nil];
}
有没有其他人做过类似的事情,并能为我提供更好的解决方案?