Это может быть полезно для вас. Вы можете попробовать этот пример.
Создать SecondViewController и установить его имя идентификатора. И загрузите его нажатием кнопки с вашего основного ViewController.
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func gotoNextPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
vc.view.backgroundColor = .yellow
GlobalVariable.transitionViewFrom(from: self, to: vc, subtype: .fromRight)
}
}
class GlobalVariable: NSObject {
private static var sharedManager: GlobalVariable!
public var allViewInstantController: [UIViewController]=[UIViewController]()
public static let NUMBER_OF_VIEWCONTROLLER_OPEN_AT_A_TIME:Int = 5
static func shared() -> GlobalVariable {
if(sharedManager==nil){
sharedManager=GlobalVariable()
}
return sharedManager
}
override private init() {
}
static func transitionViewFrom(from:UIViewController, to:UIViewController, subtype:CATransitionSubtype) -> Void {
let timeDelay:Double=0.5
GlobalVariable.checkPreviousView(_view:to)
GlobalVariable.splashViewFrom(from: from, to:to, isAdd:true, subtype:subtype, timeDelay: timeDelay)
}
static func checkPreviousView(_view:UIViewController) -> Void {
GlobalVariable.shared().allViewInstantController.append(_view)
if(GlobalVariable.shared().allViewInstantController.count>GlobalVariable.NUMBER_OF_VIEWCONTROLLER_OPEN_AT_A_TIME){
let myViewController = GlobalVariable.shared().allViewInstantController.first
myViewController?.view.removeFromSuperview()
GlobalVariable.shared().allViewInstantController.removeFirst()
}
}
static func splashViewFrom(from:UIViewController, to:UIViewController, isAdd:Bool, subtype:CATransitionSubtype, timeDelay:Double) -> Void {
do {
let transition = CATransition()
transition.duration = 0.25
transition.isRemovedOnCompletion = true;
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = subtype
from.view.layer.add(transition, forKey: kCATransition)
if(!isAdd){
from.view.removeFromSuperview()
if(GlobalVariable.shared().allViewInstantController.count>0){
GlobalVariable.shared().allViewInstantController.removeLast()
}
}else{
from.view.addSubview(to.view)
}
}
}
static func dismissViewFrom(viewController:UIViewController, subtype:CATransitionSubtype) -> Void {
let timeDelay=0.5
GlobalVariable.splashViewFrom(from: viewController, to: UIViewController(), isAdd:false, subtype:subtype, timeDelay: timeDelay)
}
}
SecondViewController.swift
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func gotoPreviousPage(_ sender: Any) {
GlobalVariable.dismissViewFrom(viewController: self, subtype: CATransitionSubtype.fromLeft)
}
}