2016-05-27 356 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) 
}