キーボードアニメーションの問題モーダルビューコントローラー内でbecledfirstresponderを呼び出す際の問題

StackOverflow https://stackoverflow.com/questions/2512502

質問

私は電話にいくつかの問題を抱えています -becomeFirstResponder aで UITextField モダンに提示されたビューコントローラーが含まれています。モーダルビューコントローラーでこの方法を呼び出します -viewDidLoad キーボードがすぐに表示されるように方法。私が期待していたのは、キーボードとモーダルビューコントローラーの両方が、画面の下部から同時にアニメーション化することです。しかし、私が観察しているのは次のとおりです。

  1. を呼び出すボタンをクリックするまでに〜0.2秒のUIラグがあります -presentModalViewController:animated: 親ビューコントローラーのメソッドと、チャイルドビューコントローラーがモダンにアニメーション化し始めたとき。
  2. キーボードは、モーダルビューコントローラーのアニメーションが始まるとすぐに、すぐにアニメーションがまったく表示されません。
  3. モーダルビューコントローラーのアニメーションが完了すると、他のすべてがスムーズに動作するように見えます。
  4. モーダルビューコントローラーを却下すると、画面からスムーズにアニメーション化されます(偶然にキーボードとともに)。
  5. 初めてモーダルビューコントローラーを提示するボタンをクリックすると、〜0.2秒のUIラグがないことを除いて、同じパターンが発生します。

キーボードのアニメーションとモーダルビューコントローラーのアニメーションが同時にいくつかの低レベルのコアアニメーションリソースを競い合っているかのようですが、なぜこれが起こるのかはわかりません。この予感を裏付けると思われるのは、私が尋ねなければ UITextField 最初のレスポンダーになるために(つまり、キーボードに自分自身を提示するように依頼しない場合)、UIラグはまったくありません。モーダルビューコントローラーは即座にアニメーション化します。

興味深いことに、私がようなことをするなら [self.textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0001]; その後、キーボードのアニメーションは、Modal View Controllerのアニメーションとほぼ同時に発生します。IPhoneシミュレーターで実行するときに同時に両方ともアニメーション化されていないことを知ることは非常に困難です。ただし、実際のデバイスで実行する場合、モーダルビューコントローラーが表示されるまでキーボードが表示されないことは容易に顕著です。重要なことに、これ以上の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.

他のヒント

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

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.

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.

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top