前言
我有一个应用程序,其中包含一个聊天部分,我正在将键盘隐藏和显示的动画与聊天输入框的上升和下降同步。
这是我使用的代码:
SHOW:
- (void) keyboardWillShow:(NSNotification *)note {
NSDictionary *keyboardAnimationDetail = [note userInfo];
UIViewAnimationCurve animationCurve = [keyboardAnimationDetail[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGFloat duration = [keyboardAnimationDetail[UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSValue* keyboardFrameBegin = [keyboardAnimationDetail valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
// working for hardware keyboard
//UIViewAnimationOptions options = (UIViewAnimationOptions)animationCurve;
// working for virtual keyboard
UIViewAnimationOptions options = (animationCurve << 16);
[UIView animateWithDuration:duration delay:0.0 options:options animations:^{
textView.frame = CGRectMake(0, self.view.bounds.size.height - keyboardFrameBeginRect.size.height, self.view.bounds.size.width, -40);
} completion:nil];
}
隐藏:
- (void) keyboardWillHide:(NSNotification *)note {
NSDictionary *keyboardAnimationDetail = [note userInfo];
UIViewAnimationCurve animationCurve = [keyboardAnimationDetail[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGFloat duration = [keyboardAnimationDetail[UIKeyboardAnimationDurationUserInfoKey] floatValue];
// hardware keyboard
//UIViewAnimationOptions options = (UIViewAnimationOptions)animationCurve;
// virtual keyboard
UIViewAnimationOptions options = (animationCurve << 16);
[UIView animateWithDuration:duration delay:0.0 options:options animations:^{
textView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, -40);
} completion:nil];
}
这在虚拟键盘上效果很好,但如果由于连接或断开硬件键盘而调用keyboardWillShow:或keyboardWillHide:,则动画会出现延迟。我可以通过更改UIViewAnimationOptions来解决这个问题。
替换为:// Works with virtual keyboard
UIViewAnimationOptions options = (animationCurve << 16);
使用:
// working for firstResponder keyboard
UIViewAnimationOptions options = (UIViewAnimationOptions)animationCurve;
但是现在,虚拟键盘的动画出现了延迟。
我知道硬件键盘动画并不常见,也可能不是最重要的问题,但我希望一切都能正常工作!
示例
VirtualKeyboard w/ (animationCurve << 16) -- 工作正常
VirtualKeyboard w/ (UIViewAnimationOptions)animationCurve -- 出现故障
HardwareKeyboard w/ (animationCurve << 16) -- 出现故障
HardwareKeyboard w/ (UIViewAnimationOptions)animationCurve -- 工作正常
注释
在模拟器中模拟硬件键盘,使用 cmd + shft + k
是的,这在真实设备上也可以重现。
如果您需要,这是我代码的其余部分,仅供复制目的
添加文本视图
textView = [UITextView new];
textView.layer.borderWidth = 10;
textView.layer.borderColor = [UIColor blackColor].CGColor;
textView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, -40);
[self.view addSubview:textView];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
[tap addTarget:self action:@selector(handleTap:)];
[self.view addGestureRecognizer:tap];
// OBSERVE KEYBOARD
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
处理触碰事件:
- (void) handleTap:(UITapGestureRecognizer *)tap {
NSLog(@"tapped");
[textView resignFirstResponder];
}
问题:
这里到底发生了什么,有没有一种好的方法可以获得一致的动画效果而不受虚拟/硬件键盘的影响?
我知道这篇文章很长,感谢您的阅读!
UIViewAnimationOptions options = animationCurve;
,如果你要同时指定多个选项,请使用二进制 OR 运算符:options = op1 | op2 | op3;
。 - Oleg Shanyuk