У вас нет никаких ссылок, если VC2. Так что действия IBOutlet не выполняют. Вы можете попробовать этот пример. Это может быть полезно для вас.
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")
GlobalVariable.transitionViewFrom(from: self, to: vc, subtype: nil, options: [.transitionFlipFromLeft])
}
}
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? = nil, options: UIView.AnimationOptions? = nil) -> Void {
let timeDelay:Double=0.5
GlobalVariable.checkPreviousView(_view:to)
GlobalVariable.splashViewFrom(from: from, to:to, isAdd:true, subtype:subtype, options: options, 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? = nil, options: UIView.AnimationOptions? = nil, timeDelay:Double) -> Void {
do {
if let subtype = subtype {
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)
}else if let options = options {
UIView.transition(from: from.view, to: to.view, duration: timeDelay, options: options)
}
if(!isAdd){
from.view.removeFromSuperview()
if(GlobalVariable.shared().allViewInstantController.count>0){
GlobalVariable.shared().allViewInstantController.removeLast()
}
}else{
if subtype != nil {
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 previousPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
GlobalVariable.transitionViewFrom(from: self, to: vc, subtype: nil, options: [.transitionFlipFromLeft])
}
}
Для флип просто измените тип. И изменить соответственно для обратной страницы транзакции.
transition.type = CATransitionType(rawValue: "flip")
Другое решение, вы можете попробовать это.
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
GlobalVariable.shared().rootViewController=self
}
@IBAction func gotoNextPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
UIView.transition(from: self.view, to: vc.view, duration: 0.5, options: [.transitionFlipFromLeft])
}
}
class GlobalVariable: NSObject {
private static var sharedManager: GlobalVariable!
public var rootViewController: UIViewController?
static func shared() -> GlobalVariable {
if(sharedManager==nil){
sharedManager=GlobalVariable()
}
return sharedManager
}
override private init() {
}
}
SecondViewController.swift
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
GlobalVariable.shared().rootViewController=self
}
@IBAction func previousPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
UIView.transition(from: self.view, to: vc.view, duration: 0.5, options: [.transitionFlipFromLeft])
}
}