3
3

More than 5 years have passed since last update.

【Swift4】UIViewControllerで横スライドのアニメーション遷移(オリジナルアニメーション)

Last updated at Posted at 2018-07-18

下記リンクを参考に実装していてちょっとハマったので備忘録として残しておきます。

UIViewControllerAnimatedTransitioningを使って画面遷移アニメーションを作る

実装内容はリンクのものを参考にしています。
が、自作アニメーションをうまく連携してくれない。

なんでだ。。。

答えはUIViewControllerTransitioningDelegateプロトコルの該当メソッドがオプショナルになっており、実装しろエラーが発生しないため検知するのが遅くなった。という状況でした。

下記に修正版を貼り付けておきます。

リンク元

SecondViewController.swift
...

// MARK: - UIViewControllerTransitioningDelegate

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        kAnimator.presenting = true // 遷移してくるときにtrueにする

        return kAnimator
    }

    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        kAnimator.presenting = false // 遷移元に戻るときにfalseにする

        return kAnimator
    }
}

修正版

SecondViewController.swift
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        kAnimator.presenting = true // 遷移してくるときにtrueにする
        // この画面に遷移してくるときに呼ばれるメソッド
        return kAnimator
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        kAnimator.presenting = false // 遷移元に戻るときにfalseにする
        // この画面から遷移元に戻るときに呼ばれるメソッド
        return kAnimator
    }

プロトコルを実装する際は定義情報を確認してメソッドにあった実装を心がけよう。うん。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3