- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
所以我有一个以聊天部分为特色的应用程序,我正在同步键盘隐藏和显示的动画与聊天输入的上升和下降。
这是我使用的代码:
显示:
- (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;
但是有了这个,现在虚拟键盘动画滞后了
我意识到硬件键盘动画不是很常见,这可能不是最重要的问题,但我喜欢一切正常工作!
虚拟键盘 w/(animationCurve << 16) -- 工作
带 (UIViewAnimationOptions)animationCurve 的虚拟键盘 -- 损坏
HardwareKeyboard w/(animationCurve << 16) -- 坏了
带 (UIViewAnimationOptions)animationCurve 的硬件键盘 -- 工作
在模拟器中模拟硬件键盘 cmd + shft + k
是的,这可以在真实设备上复制。
如果您需要,这是我的其余代码,仅用于复制目的
添加 TextView
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];
}
这是怎么回事,是否有一种无论虚拟/硬件键盘如何都能获得一致动画的好方法?
我知道这很长,感谢您的阅读!
最佳答案
由于 Apple 在键盘通知中发送给您的动画曲线没有相应的 UIViewAnimationOption 位,因此您需要下拉到老式非 block 动画并直接使用曲线:
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];
关于ios - 在 keyboardWillShow keyboardWillHide 中同步动画——同时使用硬件键盘和虚拟键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260711/
我有: override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) Notifica
我已经能够使用 keyboardWillShow 方法更新我的 scrollView 的布局约束。但是,当尝试在 keyboardWillHide 方法中执行相同操作时,布局不会更新(导致 scrol
我想在键盘隐藏时启用一个按钮。我如何使用 rxSwift 做到这一点?我试过这段代码,但从未调用过闭包: NotificationCenter.default.rx.notification(UIRe
目前我有一个 View 在键盘显示或隐藏时使用键盘进行动画处理。我添加了一个手势识别器,这样当用户点击键盘时它就会消失。 我遇到的问题是,如果用户在键盘出现时点击以降低键盘,键盘就会消失,我的 Vie
我有一个要求,我需要在其中添加一个与设备键盘具有相同高度的 View 。 由于不同的设备有不同的高度,有没有办法在不注册键盘通知的情况下获取设备的键盘高度? 如果用户在应用中的任意位置打开键盘,我可以
我有一个带有自定义 View 的 xib 文件,上面有很多文本字段 subview 。我已经将每个文本字段的委托(delegate)设置为文件所有者,并且能够成功地使用 texfield 委托(del
序言 所以我有一个以聊天部分为特色的应用程序,我正在同步键盘隐藏和显示的动画与聊天输入的上升和下降。 这是我使用的代码: 显示: - (void) keyboardWillShow:(NSNotifi
我正在尝试将 View 停靠在 iOS 和 Android 的键盘顶部。我目前已经使用 DeviceEventEmitter 进行了此操作,但我从未在 Android 上收到 keyboardWill
在做项目的时候,我遇到了这个问题。 其中一个 Controller 实现了keyboardWillShow 和keyboardWillHide(来自 Apple 的标准代码 Managing the
我是一名优秀的程序员,十分优秀!