Log*_*gan 15 animation objective-c uikeyboard uiviewanimation ios
所以我有一个具有聊天部分的应用程序,我正在同步键盘的动画隐藏和显示聊天输入的上升和下降.
这是我正在使用的代码:
节目:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
隐藏:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
这适用于虚拟键盘,但如果由于断开或连接硬件键盘而调用keyboardWillShow:或keyboardWillHide:则动画滞后.我可以通过更改UIViewAnimationOptions来解决这个问题
更换:
// Works with virtual keyboard
UIViewAnimationOptions options = (animationCurve << 16);
Run Code Online (Sandbox Code Playgroud)
附:
// working for firstResponder keyboard
UIViewAnimationOptions options = (UIViewAnimationOptions)animationCurve;
Run Code Online (Sandbox Code Playgroud)
但有了这个,现在virtualKeyboard动画落后了
我意识到硬件键盘动画并不常见,它可能不是最重要的问题,但我喜欢一切工作!
VirtualKeyboard w /(animationCurve << 16) - 工作
VirtualKeyboard w /(UIViewAnimationOptions)animationCurve - BROKEN
HardwareKeyboard w /(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, 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];
Run Code Online (Sandbox Code Playgroud)
手柄:
- (void) handleTap:(UITapGestureRecognizer *)tap {
NSLog(@"tapped");
[textView resignFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么,是否有一个很好的方法来获得一致的动画,无论虚拟/硬件键盘如何?
我意识到这很长,谢谢你的阅读!
由于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];
Run Code Online (Sandbox Code Playgroud)