iOS 11以降で UIViewControllerTransitionCoordinator によるアニメーションが初回だけ効かない問題

1.2K Views

December 01, 19

スライド概要

potatotips #55で共有した、「iOS 11以降で UIViewControllerTransitionCoordinator によるアニメーションが初回だけ効かない問題」の内容です

profile-image

2023年10月からSpeaker Deckに移行しました。最新情報はこちらをご覧ください。 https://speakerdeck.com/lycorptech_jp

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

(ダウンロード不可)

関連スライド

各ページのテキスト
1.

iOS 11以降で UIViewControllerTransitionCoordinator によるアニメーションが初回だけ効かない問題 Kazuhiro Hayashi (@kazuhiro494949) potatotips #55

2.

ナビゲーションによる画面遷移 First View Controller Transition これの色と形 が変化する First View Contro Transition Second View Second View Controller

3.

UIViewControllerTransitionCoordinatorへ 遷移時にアニメーション処理を渡す extension NavigationController: UINavigationControllerDelegate { func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { if transitionCoordinator?.viewController(forKey: .from) is SecondViewController { transitionCoordinator?.animateAlongsideTransition(in: navigationController.view, animation: { [globalView] (_) in globalView.layer.cornerRadius = 0 globalView.backgroundColor = .red }, completion: { context in }) } else if transitionCoordinator?.viewController(forKey: .to) is SecondViewController { transitionCoordinator?.animateAlongsideTransition(in: navigationController.view, animation: { [globalView] (_) in globalView.layer.cornerRadius = 40 globalView.backgroundColor = .blue }, completion: nil) } } } NavigationControllerの遷移イベント前にアニメーション処理を追加

4.

デモ

5.

起動直後の初回だけ何故か効かない First View Controller Transition First View Controller Transition Seco Second View Controller

6.

原因

7.

iOSのバグっぽい

8.

Open Radar Community bug reports Animating with UIViewControllerTransitionCoordinator doesn't work first time Originator: simonbs Number: rdar://38135706 Date Originated: March 5 2018, 12:25 PM Status: Open Resolved: Product: iOS + SDK Product Version: iOS 11.2 Classification: Reproducible: Always Area: UIKit Summary: When using the transitionCoordinator of a UINavigationController the first time a view controller is pushed onto the navigation stack, the supplied animations are either not performed animated or the transition is EXTREMELY fast. The animations in the following pops and pushes are performed animated. https://openradar.appspot.com/38135706

9.

対策

10.

遷移アニメーションそのものを UIViewControllerAnimatedTransitioning でのカスタムトランジションへ差し替える

11.
[beta]
extension NavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation:
UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) ->
UIViewControllerAnimatedTransitioning? {
return operation == .push ? CustomPushAnimator() : CustomPopAnimator()
}
}
UIViewControllerAnimatedTransitioningを実装したクラスを遷移アニメーションとして返す
12.

デモ

13.

このようにカスタムトランジション にすることで動くようになった First View Controller Transition First View Controller Transition Seco Second View Controller

14.

Q. じゃあUIViewControllerTransitionCoordinator を使わずカスタムトランジション内で 全てのアニメーションを実行すればいいのでは?

15.

A. はい

16.

まとめ ・iOS11・iOS12ではUIViewControllerTransitionCoordinatorの アニメーションが初回遷移でだけ効かないバグがある ・対策としてはデフォルトの遷移をカスタムトランジションへ差し 替えると初回でも効くようになる

17.

デモ用コード https://github.com/kazuhiro4949/UIViewControllerTransitionCoordinatorBug

18.

参考資料 ・Animating with UIViewControllerTransitionCoordinator doesn't work first time ・https://openradar.appspot.com/38135706 ・UIViewControllerTransitionCoordinator ・https://developer.apple.com/documentation/uikit/ uiviewcontrollertransitioncoordinator