在模态视图控制器中调用 becomeFirstResponder 时的键

时间:2023-03-06
本文介绍了在模态视图控制器中调用 becomeFirstResponder 时的键盘动画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用 -becomeFirstResponder 时遇到了一些问题,该 UITextField 包含在模态显示的视图控制器中.我在模态视图控制器的 -viewDidLoad 方法中调用此方法,以便立即显示键盘.我期望键盘和模态视图控制器同时从屏幕底部开始动画.但是,我观察到的是以下内容:

I've been having some issues with calling -becomeFirstResponder on a UITextField contained with a view controller that is presented modally. I call this method in the modal view controller's -viewDidLoad method so that the keyboard is immediately displayed. What I expected is for both the keyboard and the modal view controller to animate from up the bottom of the screen at the same time. However, what I'm observing is the following:

  1. 在单击父视图控制器上调用 -presentModalViewController:animated: 方法的按钮与子视图控制器开始模态动画之间存在约 0.2 秒的 UI 延迟.
  2. 模态视图控制器的动画一开始,键盘就会立即呈现,完全没有动画.
  3. 一旦模态视图控制器的动画完成,其他一切似乎都可以顺利运行.
  4. 关闭模态视图控制器会导致它在屏幕外平滑地动画化(巧合的是,与键盘一起).
  5. 在第一次之后的任何时间单击显示模态视图控制器的按钮都会产生相同的模式,只是没有约 0.2 秒的 UI 延迟.
  1. There is a ~0.2 second UI lag between clicking the button that calls the -presentModalViewController:animated: method on the parent view controller and when the child view controller begins to animate modally.
  2. The keyboard is immediately presented with absolutely no animation as soon as the modal view controller's animation begins.
  3. Once the modal view controller's animation is complete, everything else seems to operate smoothly.
  4. Dismissing the modal view controller results in it being smoothly animated off screen (along with the keyboard, coincidentally).
  5. Clicking the button that presents the modal view controller any time after the first time results in the same pattern except that there is no ~0.2 second UI lag.

就好像键盘的动画和模态视图控制器的动画同时竞争一些较低级别的核心动画资源,但我不明白为什么会发生这种情况.似乎进一步证实了这种预感的是,如果我不要求 UITextField 成为第一响应者(即,如果我不要求键盘呈现自己),那么绝对没有 UI滞后,模态视图控制器立即动画.

It's as if the keyboard's animation and the modal view controller's animation are both competing for some lower-level Core Animation resource at the same time, but I don't see why this should be happening. What further seems to corroborate this hunch is if I don't ask the UITextField to become the first responder (i.e., if I don't ask the keyboard to present itself), then there is absolutely no UI lag, and the modal view controller animates instantly.

有趣的是,如果我执行类似 [self.textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0001]; 之类的操作,那么键盘的动画几乎与模态视图控制器的动画——在 iPhone 模拟器上运行时,很难说它们不是同时被动画.然而,当在实际设备上运行时,很容易注意到键盘直到模态视图控制器出现后才会出现.但重要的是,不再有 UI 延迟.

Interestingly, if I do something like [self.textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0001]; then the animation of the keyboard happens nearly at the same time as the modal view controller's animation -- it's extremely difficult to tell that they aren't both being animated at the exact same time when running on the iPhone Simulator. However, when running on an actual device, it's easily noticeable that the keyboard doesn't appear until after the modal view controller is presented. Importantly, though, there's no more UI lag.

有没有人遇到过类似的情况?

Has anyone experienced anything similar to this?

推荐答案

我相信您遇到了问题,因为您正在有效地堆叠动画.键盘视图包含在模态视图中.键盘视图试图在一个视图的上下文中为它的幻灯片设置动画,该视图本身正在为幻灯片中的幻灯片设置动画.键盘动画试图击中一个移动的目标.

I believe you're having problems because you're effectively stacking animations. The keyboard view is contained by the modal view. The keyboard view is trying to animate its slide in transition within the context of a view which is itself animating a slide in transition. The keyboard animation is trying to hit a moving target.

暂停很可能是键盘过渡动画的运行时间.我相当确定键盘动画从其他动画中抢占了优先级,以便它可以驱动 UI 的重新排列,例如滚动表格以使键盘不会覆盖已编辑的表格行.在任何情况下,键盘动画都发生在超级视图的上下文中.在模态视图的情况下尤其如此.

The pause is most likely the run time of the keyboard transition animation. I am fairly certain the the keyboard animation seizes priority from other animations so that it can drive the rearrangement of the UI e.g. scrolling a table so that the keyboard does not overlay the edited table row. In any case, the keyboard animation occurs within the context of the superview. This is especially true in the case of modal view.

因此,键盘视图会自动滑入,但由于超级视图实际上还不可见,所以您什么也看不到.当 superview 确实滑入时,键盘已经存在,因为它的动画在 superview 开始其动画之前完成.

So, the keyboard view animates itself sliding in but because the superview is not actually visible yet, you see nothing. When the superview does slide in, the keyboard is already present because its animation was completed before the superview started its animation.

简而言之,我不认为你可以真正完成你想做的事情.相反,我认为您必须首先为模态视图转换设置动画,然后运行键盘动画,否则您将不得不接受让键盘立即可见.

In short, I don't think you can actual accomplish what you want to do. Instead, I think you will have to animate the modal view transition first, then run the keyboard animation or you will have to accept having the keyboard immediately visible.

我认为上面 Cirrostratus 的建议是一个很好的建议.使用将在模态视图中滑入的键盘图像,然后立即将其与真实键盘交换.

I think that Cirrostratus' suggest above is a good one. Use an image of the keyboard that will slide in with the modal view and then immediately swap it out with the real keyboard.

这篇关于在模态视图控制器中调用 becomeFirstResponder 时的键盘动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:PhoneGap:有没有办法阻止键盘调整视图大小? 下一篇:以编程方式更改android设备的输入法

相关文章

最新文章