在模态视图控制器中调用becomeFirstResponder时的键盘动画问题 [英] Keyboard Animation Issues When Calling becomeFirstResponder within a Modal View Controller

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

问题描述

我在查看控制器所包含的 UITextField 上调用 -becomeFirstResponder 时遇到了一些问题以模态呈现。我在模态视图控制器的 -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. 点击调用<$的按钮之间有~0.2秒的UI延迟c $ c> -presentModalViewController:animated:父视图控制器上的方法以及子视图控制器开始以模态方式设置动画。

  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.

就像键盘的动画和模态视图控制器的动画一样都是com同时竞争一些较低级别的Core Animation资源,但我不明白为什么会发生这种情况。如果我不要求 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模拟器上运行时,两者都在同一时间进行动画制作。但是,当在实际设备上运行时,很容易注意到键盘在出现模态视图控制器之后才会出现。但重要的是,没有更多的用户界面滞后。

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确实滑入时,键盘已经存在,因为它的动画在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时的键盘动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆