繁体   English   中英

动画`inputView`键盘转换

[英]Animating an `inputView` Keyboard Transition

UITextField有一个inputView属性,可用于指定自定义键盘。 将此清除为nil可提供默认键盘。

如果UITextField是第一个响应者,则在设置inputView时不会发生任何更改,但通过调用[textField reloadInputView] ,键盘将立即发生更改。

我希望能够在两种不同的输入方法之间切换。 触发它的接口将是一个UISegmentedView安装为UITextFieldinputAccessoryView

我有这个工作,但过渡是非常突然的。 部分原因是我在两者之间转换的微调器和键盘在iPad上有不同的尺寸。

我发现我在reloadInputView周围包装动画块,我将在两个键盘的视图帧之间获得平滑的动画。 不幸的是,有一种视觉抖动,因为过渡不是动画:

[UIView animateWithDuration: 0.3 animations:^()
{
    [firstResponder reloadInputViews];
}];

或者,如果我在转换中包装重新加载,我可以得到一个很好的交叉淡入淡出,但我没有得到平滑的帧更改:

[UIView transitionWithView: keyboardWindow
                  duration: 0.3
                   options: UIViewAnimationOptionTransitionCrossDissolve
                animations: ^(){ [firstResponder reloadInputViews]; }
                completion: nil];

(在第二个代码块中,我从self.window获得了keyboardWindow ,因为这个视图是作为UITextFieldinputAccessoryView安装的,它最终嵌套在键盘的窗口下。)

我想要的是动画转换输入视图重新加载。 我尝试将重新加载放在动画的过渡中,我也尝试将重新加载放在过渡中的动画中 - 似乎都没有帮助。

有任何想法吗? 谢谢!

我能够通过以下方式成功动画对inputView更改,而不会出现任何故障:

  1. 添加新的inputView作为现有inputView的子视图然后
  2. 完成所选动画后,将UIControlinputView切换到自定义inputView

作为一个例子,这里是如何快速淡入自定义inputView

//start the view as invisible
_customInputView.alpha = 0.0f;

//add the custom inputView as a subview of the existing inputView
[self.inputView addSubview:_customInputView];

//animate the opacity change
[UIView animateWithDuration:0.25f 
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         _customInputView.alpha = 1.0f;
                     } completion:^(BOOL finished) {
                         //switch the UIControl's inputView
                         self.inputView = _customInputView;
                     }];

您可以调用resignFirstResponder以使输入视图消失,然后在延迟之后调用becomeFirstResponder以使键盘出现,反之亦然。

在重新签名普通键盘后,以下设置为inputView。

let delayTime = dispatch_time(DISPATCH_TIME_NOW,Int64(1 * (NSEC_PER_SEC/3)))

textField.resignFirstResponder()

dispatch_after(delayTime, dispatch_get_main_queue()) {
     self.textField.inputView = currentInputView
     self.textField.becomeFirstResponder()
} 

这设置为键盘。

dispatch_after(delayTime, dispatch_get_main_queue()) {
     self.textField.inputView = nil
     self.textField.becomeFirstResponder()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM