Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 快速翻转视图控制器将禁用IBOutlet_Swift_Uianimation - Fatal编程技术网

Swift 快速翻转视图控制器将禁用IBOutlet

Swift 快速翻转视图控制器将禁用IBOutlet,swift,uianimation,Swift,Uianimation,我的应用程序启动,我将在tabBarController的顶部显示VC1 tabBarController?.present(VC1,动画:false,完成:nil) 它显示第一页,我有另一页VC2。 我通过向tabBarController中当前打开的VC0发布通知(不是显示的VC0,而是tabBarController中索引0中的VC0)在这两个页面之间进行翻转转换。通知触发: if var topController = UIApplication.shared.keyWindow

我的应用程序启动,我将在tabBarController的顶部显示VC1

tabBarController?.present(VC1,动画:false,完成:nil)

它显示第一页,我有另一页VC2。 我通过向tabBarController中当前打开的VC0发布通知(不是显示的VC0,而是tabBarController中索引0中的VC0)在这两个页面之间进行翻转转换。通知触发:

    if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        let VC2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "VC2") as! VC2
        UIView.transition(from: topController.view, to: VC2.view, duration: 0.85, options: [.transitionFlipFromLeft])
    }

这会进行翻转,但看起来IBOutlet没有连接,并且没有检测到咔嗒声。

如果是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)
    }
}
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])
    }
}
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() {
    }
}
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])
    }
}
斯威夫特

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)
    }
}
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])
    }
}
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() {
    }
}
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])
    }
}
对于翻转,只需更改类型。并对后台事务进行相应更改

transition.type = CATransitionType(rawValue: "flip")
另一个解决方案,你可以试试这个

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)
    }
}
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])
    }
}
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() {
    }
}
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])
    }
}
斯威夫特

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)
    }
}
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])
    }
}
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() {
    }
}
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])
    }
}

谢谢,但我需要的是视图翻转,而不是页面滚动。这两种解决方案都使我的代码带有非活动的@iboutler。昨天你给我的密码起作用了。可能问题是我正在另一个视图控制器中执行GlobalVariable,并使用topController发出通知。。。Idk…是的,现在我用昨天的代码更改了transition.type=catTransitionType(rawValue:“flip”),它成功了。