How to change the Push and Pop animations in a navigation based app

前端 未结 25 1168
清酒与你
清酒与你 2020-11-22 12:46

I have a navigation based application and I want to change the animation of the push and pop animations. How would I do that?

Edit 2018

Ther

25条回答
  •  太阳男子
    2020-11-22 13:02

    @Luca Davanzo's answer in Swift 4.2

    public extension UINavigationController {
    
        /**
         Pop current view controller to previous view controller.
    
         - parameter type:     transition animation type.
         - parameter duration: transition animation duration.
         */
        func pop(transitionType type: CATransitionType = .fade, duration: CFTimeInterval = 0.3) {
            self.addTransition(transitionType: type, duration: duration)
            self.popViewController(animated: false)
        }
    
        /**
         Push a new view controller on the view controllers's stack.
    
         - parameter vc:       view controller to push.
         - parameter type:     transition animation type.
         - parameter duration: transition animation duration.
         */
        func push(viewController vc: UIViewController, transitionType type: CATransitionType = .fade, duration: CFTimeInterval = 0.3) {
            self.addTransition(transitionType: type, duration: duration)
            self.pushViewController(vc, animated: false)
        }
    
        private func addTransition(transitionType type: CATransitionType = .fade, duration: CFTimeInterval = 0.3) {
            let transition = CATransition()
            transition.duration = duration
            transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            transition.type = type
            self.view.layer.add(transition, forKey: nil)
        }
    
    }
    

提交回复
热议问题