前言
所以我有一个应用程序的聊天部分,我正在同步的键盘的动画隐藏和显示与上升和下降的聊天输入.
这是我使用的代码:
显示:
- (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,-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 – BROKEN 硬件键盘/(animationCurve<< 16) - BROKEN HardwareKeyboard w /(UIViewAnimationOptions)animationCurve – WORKING 笔记 模拟器中的硬件键盘cmd shft k 是的,这可以在真实设备上复制. 如果你想要它,这里是我的代码的其余部分,仅用于复制目的 添加文字视图
textView = [UITextView new]; textView.layer.borderWidth = 10; textView.layer.borderColor = [UIColor blackColor].CGColor; textView.frame = CGRectMake(0,-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]; }
问题:
这里发生了什么,无论虚拟/硬件键盘如何,都有一个很好的方式来获得一致的动画?
我意识到这很久,谢谢你的阅读!
解决方法
由于Apple在键盘通知中发送的动画曲线没有相应的UIViewAnimationOption位,因此您需要下载到旧的非块动画并直接使用曲线:
NSTimeInterval duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; [UIView beginAnimations:@"SomeAnimationID" context:NULL]; [UIView setAnimationCurve:curve]; [UIView setAnimationDuration:duration]; // Animation code [UIView commitAnimations];