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

问题描述 投票:0回答:1

我想在导航视图控制器上推动视图控制器以及以下动画效果。

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

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

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

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

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

ios swift animation uinavigationcontroller
1个回答
0
投票

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

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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.