1.2K Views
December 01, 19
スライド概要
potatotips #55で共有した、「iOS 11以降で UIViewControllerTransitionCoordinator によるアニメーションが初回だけ効かない問題」の内容です
2023年10月からSpeaker Deckに移行しました。最新情報はこちらをご覧ください。 https://speakerdeck.com/lycorptech_jp
iOS 11以降で UIViewControllerTransitionCoordinator によるアニメーションが初回だけ効かない問題 Kazuhiro Hayashi (@kazuhiro494949) potatotips #55
ナビゲーションによる画面遷移 First View Controller Transition これの色と形 が変化する First View Contro Transition Second View Second View Controller
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の遷移イベント前にアニメーション処理を追加
デモ
起動直後の初回だけ何故か効かない First View Controller Transition First View Controller Transition Seco Second View Controller
原因
iOSのバグっぽい
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
対策
遷移アニメーションそのものを UIViewControllerAnimatedTransitioning でのカスタムトランジションへ差し替える
extension NavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation:
UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) ->
UIViewControllerAnimatedTransitioning? {
return operation == .push ? CustomPushAnimator() : CustomPopAnimator()
}
}
UIViewControllerAnimatedTransitioningを実装したクラスを遷移アニメーションとして返す
デモ
このようにカスタムトランジション にすることで動くようになった First View Controller Transition First View Controller Transition Seco Second View Controller
Q. じゃあUIViewControllerTransitionCoordinator を使わずカスタムトランジション内で 全てのアニメーションを実行すればいいのでは?
A. はい
まとめ ・iOS11・iOS12ではUIViewControllerTransitionCoordinatorの アニメーションが初回遷移でだけ効かないバグがある ・対策としてはデフォルトの遷移をカスタムトランジションへ差し 替えると初回でも効くようになる
デモ用コード https://github.com/kazuhiro4949/UIViewControllerTransitionCoordinatorBug
参考資料 ・Animating with UIViewControllerTransitionCoordinator doesn't work first time ・https://openradar.appspot.com/38135706 ・UIViewControllerTransitionCoordinator ・https://developer.apple.com/documentation/uikit/ uiviewcontrollertransitioncoordinator