ios – 在KeyboardWillShow中同步动画KeyboardWillHide – 硬件键盘和虚拟键盘同时

ios – 在KeyboardWillShow中同步动画KeyboardWillHide – 硬件键盘和虚拟键盘同时,第1张

概述前言 所以我有一个应用程序的聊天部分,我正在同步的键盘的动画隐藏和显示与上升和下降的聊天输入. 这是我使用的代码: 显示: - (void) keyboardWillShow:(NSNotification *)note { NSDictionary *keyboardAnimationDetail = [note userInfo]; UIViewAnimationCurve 前言

所以我有一个应用程序的聊天部分,我正在同步的键盘的动画隐藏和显示与上升和下降的聊天输入.

这是我使用的代码:

显示:

- (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 keyboardUIVIEwAnimationoptions options = (animationCurve << 16);

附:

// working for firstResponder keyboardUIVIEwAnimationoptions 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];
总结

以上是内存溢出为你收集整理的ios – 在KeyboardWillShow中同步动画KeyboardWillHide – 硬件键盘和虚拟键盘同时全部内容,希望文章能够帮你解决ios – 在KeyboardWillShow中同步动画KeyboardWillHide – 硬件键盘和虚拟键盘同时所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1112254.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存