LoginSignup
1
3

More than 5 years have passed since last update.

上から下への画面遷移アニメーション

Last updated at Posted at 2018-04-05

概要

defaultでのsegueのアニメーションが下から上に画面が出るものだったので、戻るときはその逆である上から下のアニメーションを実装したいと思い、実装してみた

コード

TopToBottomStoryBoardSegue

import UIKit

class TopToBottomSegue: UIStoryboardSegue {
    override func perform() {
        let firstVCView = self.source.view as UIView?
        let secondVCView = self.destination.view as UIView?

        // 画面の大きさを取得
        let screenWidth = UIScreen.main.bounds.size.width
        let screenHeight = UIScreen.main.bounds.size.height

        // 次出てくるviewを最初に今のviewの後ろ側に表示
        secondVCView?.frame = CGRect(x: 0,y: 0,width: screenWidth, height: screenHeight)
        let window = UIApplication.shared.keyWindow
        window?.insertSubview(secondVCView!, at: 0)

        // 邪魔になっている今のviewを画面外まで下げれば終了
        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            firstVCView?.center.y += screenHeight
        }) { (Finished) -> Void in
            self.source.present(self.destination as! UIViewController,
                                                            animated: false,
                                                            completion: nil)
        }

}
}

1
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
1
3