在模态视图控制器中调用 comeFirstResponder 时出现键盘动画问题

发布于 2024-08-26 15:20:03 字数 920 浏览 7 评论 0原文

我在以模态方式呈现的视图控制器包含的 UITextField 上调用 -becomeFirstResponder 时遇到了一些问题。我在模态视图控制器的 -viewDidLoad 方法中调用此方法,以便立即显示键盘。我期望键盘和模态视图控制器同时从屏幕底部开始产生动画。然而,我观察到的是:

  1. 在父视图控制器上单击调用 -presentModalViewController:animated: 方法的按钮与子视图控制器之间有约 0.2 秒的 UI 延迟开始模态动画。
  2. 一旦模态视图控制器的动画开始,键盘就会立即呈现完全没有动画。
  3. 一旦模态视图控制器的动画完成,其他一切似乎都运行顺利。
  4. 关闭模态视图控制器会导致它在屏幕外平滑地动画化(巧合的是,还有键盘)。
  5. 在第一次之后的任何时间单击呈现模态视图控制器的按钮都会导致相同的模式,只是没有 ~0.2 秒的 UI 延迟。

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

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

有没有人经历过类似的事情?

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. 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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我还不会笑 2024-09-02 15:20:03

我相信您遇到问题是因为您正在有效地堆叠动画。键盘视图包含在模式视图中。键盘视图试图在视图的上下文中为其幻灯片过渡设置动画,该视图本身也为过渡幻灯片设置动画。键盘动画试图击中移动目标。

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

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

简而言之,我认为你无法真正完成你想做的事情。相反,我认为您必须首先对模式视图过渡进行动画处理,然后运行键盘动画,否则您将不得不接受让键盘立即可见。

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

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.

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.

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.

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.

拒绝两难 2024-09-02 15:20:03

延迟的键盘动画也让我烦恼。 viewDidLayoutSubviews 是我一直在寻找的神奇方法。将 becomeFirstResponder 调用放在那里可以使键盘与模式一起及时向上滑动。

https://stackoverflow.com/a/19517739/3618864

The delayed keyboard animation bothered me as well. viewDidLayoutSubviews was the magical method I was looking for. Placing the becomeFirstResponder call there makes the keyboard slide up in time with the modal.

https://stackoverflow.com/a/19517739/3618864

甜柠檬 2024-09-02 15:20:03

尝试将发送 beginResponder 的代码移出 viewDidLoad 并移至 viewWillAppear 中。我认为现在开始太早了,您希望键盘动画在视图出现动画发生时发生。

Try moving your code that sends becomeFirstResponder out of viewDidLoad and into viewWillAppear. I think it is starting too early, you want the keyboard animation to happen when the view appearing animation happens.

您的好友蓝忘机已上羡 2024-09-02 15:20:03

您是说您在模拟器上看到了延迟,但在设备上却没有看到延迟?如果是这种情况,您可能会遇到延迟,因为您的计算机需要时间将所有内容加载到内存中。第一次加载模拟器时,它不仅仅是本机运行代码,它可能会加载各种运行时和调试库。一旦加载到内存中,系统可能会相当快。如果您在模拟器上遇到延迟,也许您的开发机器中更多的 RAM 可能会有所帮助。如果您的机器已经使用了几年,您可能会考虑购买新的机器。

Are you saying that you are seeing lag on the Simulator but not on the device? If that's the case you might be seeing a lag due to your computer taking it's time loading everything into memory. When loading up the Simulator the first time it's not just running the code natively, it's probably loading all manner of runtime and debugging libraries. Once loaded into memory the system is probably rather fast. If you are experiancing lag on the Simulator maybe some more RAM in your dev machine might help. If your machine is a few years old you might think about going for something new.

孤云独去闲 2024-09-02 15:20:03

这就是我所做的,使键盘看起来与模态视图控制器的动画完全相同:

在以模态方式呈现的视图的 (init) 方法中,我创建了 UITextField 并将其设为第一响应者。然后,当我向模态视图控制器呈现动画时,它们会同时出现。

This is what I did to make the keyboard appear to animate exactly the same time as a modalviewcontroller:

In the (init) method of the view being presented modally, I created the UITextField and made it the first responder. Then when I present the modal view controller with animation they both appear at the same time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文