2017-07-26 111 views
1

我有一個導航控制器,並在該導航控制器的內部,我有一個主屏幕,從主屏幕我點擊一個按鈕,轉到另一個屏幕。自定義轉換

但使用導航控制器時的標準顯示動畫是從側面滑動,但我想要做的是視圖控制器從屏幕底部向上滑動,並在到達時創建一種彈跳動畫頂端。

+1

你到目前爲止嘗試過什麼?顯示您嘗試使用的代碼,並解釋您遇到問題的位置。如果你還沒有嘗試過任何東西,跳到谷歌(或你最喜歡的搜索引擎)並搜索'uinavigationcontroller自定義轉換... ...你會發現很多的例子,討論,教程等等,等等。 – DonMag

回答

0

任何想使用自定義轉換的人都需要記住UIViewControllerAnimatedTransitioningUIViewControllerTransitioningDelegate協議。現在,順應UIViewControllerAnimatedTransitioning您customclass內從NSObject

import UIKit 
class CustomPushAnimation: NSObject, UIViewControllerAnimatedTransitioning { 

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
     return 0.2 
    } 

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
     let containerVw = transitionContext.containerView 
     let fromViewController = transitionContext.viewController(forKey: .from) 
     let toViewController = transitionContext.viewController(forKey: .to) 
     guard let fromVc = fromViewController, let toVc = toViewController else { return } 
     let finalFrame = transitionContext.finalFrame(for: toVc) 

    //For different animation you can play around this line by changing frame 
     toVc.view.frame = finalFrame.offsetBy(dx: 0, dy: finalFrame.size.height/2) 
     containerVw.addSubview(toVc.view) 
     UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { 
      toVc.view.frame = finalFrame 
      fromVc.view.alpha = 0.5 
     }, completion: {(finished) in 
      transitionContext.completeTransition(finished) 
      fromVc.view.alpha = 1.0 
     }) 
    } 
} 

上述方法繼承將動畫的照顧。之後,創建 上述目的和使用yourViewController類中

import UIKit 
class YourViewController: UIViewController { 
lazy var customPushAnimation: CustomPushAnimation = { 
     return CustomPushAnimation() 
    }() 
func openViewControler() { 
}let vc =//Assuming your view controller which you want to open 
let navigationController = UINavigationController(rootViewController: vc) 
//Set transitioningDelegate to invoke protocol method 
navigationController.transitioningDelegate = self 
present(navigationController, animated: true, completion: nil) 
} 

注:爲了看到動畫。在呈現ViewController時,千萬不要將動畫標誌設置爲假 。否則,你的動畫將不會工作, 。

最後實現UIViewControllerTransitioningDelegate協議方法裏面YourViewcontroller

extension YourViewController: UIViewControllerTransitioningDelegate { 
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return customPushAnimation 
    } 

每當您提出上述協議的方法 打來電話,你的動畫魔力就會出現視圖 - 控制。