如何使用滑动动画效果推送和弹出视图控制器?

时间:2020-02-28 10:41:41

标签: ios swift animation uinavigationcontroller

我想在导航视图控制器上同时推送视图控制器和以下动画效果。

我通过使用导航控制器委托方法尝试了此示例,但无法获得那种滑动效果。

https://gist.github.com/eoghain/7e9afdd43d1357fb8824126e0cbd491d

请在下面的示例GIF中检查我到底需要哪种类型的动画。

请查看导航栏,我只是向左/向右滑动,它正在执行视图控制器的推/弹出操作。

有人可以在这方面帮助我吗?还请让我知道是否可以使用UIPageViewController?如果是,那么我如何保持顶部导航栏像GIF一样。

1 个答案:

答案 0 :(得分:0)

这可能对您有帮助。您可以尝试这个示例。

创建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)
    }
}