在KeyboardWillShow中同步动画keyboardWillHide - 硬件键盘&虚拟键盘同时 [英] Synchronizing Animations in keyboardWillShow keyboardWillHide -- Hardware Keyboard & Virtual Keyboard Simultaneously
问题描述
所以我有一个带聊天部分的应用程序,我正在同步键盘的动画隐藏和显示聊天的起伏输入。
So I have an application featuring a chat section, and I'm synchronizing the animation of the keyboard hiding and showing with the rise and fall of the chat input.
这是我正在使用的代码:
Here's the code I'm using:
显示:
- (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];
}
HIDE:
- (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来解决这个问题
This works great with the virtual keyboard, but if keyboardWillShow: or keyboardWillHide: is called as a result of disconnecting or connecting a hardware keyboard, the animation lags. I can fix this by changing the UIViewAnimationOptions
替换:
// Works with virtual keyboard
UIViewAnimationOptions options = (animationCurve << 16);
使用:
// working for firstResponder keyboard
UIViewAnimationOptions options = (UIViewAnimationOptions)animationCurve;
但有了这个,现在virtualKeyboard动画落后
But with this, now the virtualKeyboard animation lags
我意识到硬件键盘动画并不常见,它可能不是最重要的问题,但我喜欢一切正常工作!
I realize that hardware keyboard animations aren't very common and it's perhaps not the most important issue, but I like everything to just work!
VirtualKeyboard w /(animationCurve<< 16) - WORKING
VirtualKeyboard w/ (animationCurve << 16) -- WORKING
VirtualKeyboard w /(UIViewAnimationOptions)animationCurve - BROKEN
VirtualKeyboard w/ (UIViewAnimationOptions)animationCurve -- BROKEN
HardwareKeyboard w /(animationCurve<< 16) - BROKEN
HardwareKeyboard w/ (animationCurve << 16) -- BROKEN
HardwareKeyboard w / (UIViewAnimationOptions)animationCurve - WORKING
HardwareKeyboard w/ (UIViewAnimationOptions)animationCurve -- WORKING
模拟模拟器中的硬件键盘cmd + shft + k
To simulate hardware keyboard in simulator cmd + shft + k
是的,这可以在真实的情况下复制设备。
Yes, this is replicable on real device.
如果你需要它,这是我的其余代码,仅用于复制目的
In case you want it, here's the rest of my code, just for replication purposes
ADD文字视图
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];
HANDLE TAP:
HANDLE TAP:
- (void) handleTap:(UITapGestureRecognizer *)tap {
NSLog(@"tapped");
[textView resignFirstResponder];
}
问题:
这里发生了什么,有没有一种很好的方法来获得一致的动画而不管虚拟/硬件键盘?
The Question:
What is going on here, and is there a good way to get consistent animation regardless of virtual / hardware keyboard?
我意识到这很长,谢谢你的阅读!
I realize this is long, thank you for reading!
推荐答案
由于Apple在键盘通知中发送给你的动画曲线没有相应的UIViewAnimationOption位,你需要下拉到老式非块动画并直接使用曲线:
Since the animation curve Apple sends you in the keyboard notification does not have a corresponding UIViewAnimationOption bit, you need to drop down to old-school non-block animations and use the curve directly:
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];
这篇关于在KeyboardWillShow中同步动画keyboardWillHide - 硬件键盘&虚拟键盘同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!