HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 在KeyboardWillShow中同步动画KeyboardWillHide – 硬件键盘和虚拟键盘同时大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
前言

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

这是我使用的代码

显示

- (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];

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。