LoginSignup
0
3

More than 5 years have passed since last update.

Swift3でカスタムクラスを使って画面遷移

Last updated at Posted at 2017-05-15

説明

画面遷移の為のカスタムクラスを作ってみました。引数に自分のStoryboardの名前を入れると簡単にpresentviewControllerで画面遷移ができます!New > File > Swift File を選んでカスタムクラスを実装して使います。

注意

ちなみに、このクラスでは、同じMain Storyboardの中にあるViewには飛べません。あくまでも別のStoryboardにあるViewControllerにしか飛べません。(不便ですみません)同じStoryboardの中にあるViewControllerに飛ぶ為のクラスはまた暇を見つけて作ろうと思いました。

使い方

スクリーンショット 2017-05-15 19.41.22.png

//右に遷移
CustomScreenTransition().Right(StoryboardName: "YourStoryboardName")

//左に遷移
CustomScreenTransition().Left(StoryboardName: "YourStoryboardName")

実装

import UIKit

public class CustomScreenTransition: UIViewController {

    public func Right(StoryboardName: String) {

        let storyboard: UIStoryboard = UIStoryboard(name: (StoryboardName), bundle: nil)
        let nextView = storyboard.instantiateInitialViewController()

        let transition = CATransition()
        transition.duration = 0.5
        transition.type = kCATransitionPush

        transition.subtype = kCATransitionFromRight
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
        appDelegate?.window?.layer.add(transition, forKey: kCATransition)
        appDelegate?.window?.rootViewController = nextView!


    }

    public func Left(StoryboardName: String) {

        let storyboard: UIStoryboard = UIStoryboard(name: StoryboardName, bundle: nil)
        let nextView = storyboard.instantiateInitialViewController()

        let transition = CATransition()
        transition.duration = 0.5
        transition.type = kCATransitionPush

        transition.subtype = kCATransitionFromLeft
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
        appDelegate?.window?.layer.add(transition, forKey: kCATransition)
        appDelegate?.window?.rootViewController = nextView!

    }

}

参考

Swift - Accessing AppDelegate window from viewController

画面遷移のための関数を別のクラスから呼びたい

Swift3でカスタムクラスを使って画面遷移

0
3
1

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