LoginSignup
21
17

More than 5 years have passed since last update.

画面遷移時の独自アニメーションのメモ

Posted at

UIViewControllerAnimatedTransitioningを使う場合

正直、全然ピンと来なかったけど、以下のページが分かりやすかった。
UIViewControllerAnimatedTransitioningを使って画面遷移アニメーションを作る

後、Appleのサンプルコードが一番わかり易い。Objective-Cのコードだけど。

CustomTransitions

というサンプルをXcodeのDocumentation and API Referenceから検索して読んでみて欲しい。
※見つからない場合、preferencesのComponentsからGuides and Sample Codeからダウンロードしてみてください。

CICategoryTransitionを使う場合

あまりサンプルがないけど、これが決定版と言える
Core Image の遷移エフェクトを使う

サンプル

UIViewControllerAnimatedTransitioningを使ったサンプル。

開発環境

  • XCode8.1
  • Swift3

20161113.gif

CustomTransitionsにある複数の遷移アニメーションからCross Dissolveをswiftに直して実装。

段々透明になっていくアニメーションを用意する。
用意するアニメーションはUIViewControllerAnimatedTransitioningを適用する

Animation.swift
class Animation : NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 1.0
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let fromViewCotnroller = transitionContext.viewController(forKey: .from)
        let toViewCotnroller = transitionContext.viewController(forKey: .to)

        let containerView = transitionContext.containerView

        let fromView = fromViewCotnroller?.view
        let toView = toViewCotnroller?.view

        fromView?.frame = transitionContext.initialFrame(for: fromViewCotnroller!)
        toView?.frame = transitionContext.finalFrame(for: toViewCotnroller!)

        fromView?.alpha = 1.0
        toView?.alpha = 0.0

        containerView.addSubview(toView!)

        UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: .curveLinear, animations: { () -> Void in
                fromView?.alpha = 0.0
                toView?.alpha = 1.0
            }, completion: { (BOOL) -> Void in
                let wasCanceled = transitionContext.transitionWasCancelled
                transitionContext.completeTransition(!wasCanceled)
            })

    }


}

作ったアニメーションを遷移先のViewControllerに適用する

class ViewController: UIViewController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let nextButotn : UIButton = UIButton(frame: CGRect(x: self.view.frame.midX-50, y: self.view.frame.midY-50, width: 100, height: 100))
        nextButotn.backgroundColor = UIColor.blue
        nextButotn.setTitle("second", for: .normal)
        nextButotn.addTarget(self, action: #selector(onTapped), for: .touchUpInside)
        self.view.addSubview(nextButotn)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func onTapped(sender: UIButton){
//        NSLog("tapped")

        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let second = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController

        second.transitioningDelegate = self

        self.present(second, animated: true, completion: nil)
    }

    //遷移時アニメーション
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return Animation()
    }

    //戻る時のアニメーション
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return Animation()
    }
}

一応、完全なソースはgithubにあげてあります。
AnimatedTransitioningSample

21
17
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
21
17