2016-05-27 319 views
0

我想要做的是從左側側推動ViewController的自定義動畫。 我已經創建了我的自定義過渡委託,並提供了我的自定義動畫,並且一切正常(從左側的新視圖幻燈片)。 唯一的問題是iOS中的推動動畫不僅僅是從右側滑動視圖。被遮蔽的VC也在VC被推動的同一方向上稍微移動。此外,導航欄有點閃爍。我當然可以嘗試通過猜測參數應該是什麼來模仿這種行爲(例如,在不同的iPhone上,VC被遮擋的程度有多大),但是也許有可能在某處找到這些值? 非常感謝。PushViewController詳細信息?

回答

0

我會創造一個UIViewControllerAnimatedTransitioning協議守法的對象

class CustomHorizontalSlideTransition: NSObject, UIViewControllerAnimatedTransitioning { 

    var operation: UINavigationControllerOperation = .Push 

    convenience init(operation: UINavigationControllerOperation) { 
     self.init() 
     self.operation = operation 
    } 

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { 
     return 0.5 
    } 

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) { 

     let containerView = transitionContext.containerView() 

     let disappearingVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)! 
     let appearingVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)! 

     let bounds = UIScreen.mainScreen().bounds 

     if self.operation == .Push { 
      appearingVC.view.frame = CGRectOffset(bounds, -bounds.size.height, 0) 
      containerView!.addSubview(disappearingVC.view) 
      containerView!.addSubview(appearingVC.view) 
     } else { 
      appearingVC.view.frame = bounds 
      disappearingVC.view.frame = bounds 
      containerView!.addSubview(appearingVC.view) 
      containerView!.addSubview(disappearingVC.view) 
     } 

     UIView.animateWithDuration(transitionDuration(transitionContext), 
      delay: 0.0, 
      options: UIViewAnimationOptions.CurveEaseInOut, 
      animations: {() -> Void in 

       if self.operation == .Push { 
        appearingVC.view.frame = bounds 
       } else { 
        disappearingVC.view.frame = CGRectOffset(bounds, -bounds.size.width, 0) 
       } 

      }) { (complete) -> Void in 
       transitionContext.completeTransition(true) 
     } 
    } 
} 

然後在你的「從」和「到」視圖控制器,鑑於ViewDidAppear

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 
    navigationController?.delegate = self 
} 

中navigationController的委託設置爲自兩個視圖控制器,覆蓋以下內容以提供transitionAnimatedTransition委託方法並返回協議持續實例爲您的動畫

override func transitionAnimatedTransition(operation: UINavigationControllerOperation) -> UIViewControllerAnimatedTransitioning? { 
     return CustomHorizontalSlideTransition(operation: operation) 
}