IOS自定义转场小记

Model:

AViewController -> BViewController 从A到B

需要设置BViewControllertransitioningDelegate来控制动画,通过modalPresentationStyle来控制动画类型.
其中transitioningDelegate需要实现UIViewControllerTransitioningDelegate协议.

UIViewControllerTransitioningDelegate:
  • 如果只需实现动画无手势操作,只需实现下面PresentedDismissed两个方法.

    optional public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    optional public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    
在这两个方法中需要返回实现了UIViewControllerAnimatedTransitioning协议的对象.
  UIViewControllerAnimatedTransitioning:中需要实现两个方法
  //返回动画的时间
  public func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
  //返回动画的过程
  public func animateTransition(transitionContext: UIViewControllerContextTransitioning)
  • 如果还需要实现手势操作,除了上面两个方法外还需要实现UIViewControllerTransitioningDelegate的另外两个方法
    //百分比控制present
    optional public func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
    //百分比控制dismiss
    optional public func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
在这两个方法中需要返回UIPercentDrivenInteractiveTransition自定义子类.
  通过监听UIGestureRecognizer的过程调用UIPercentDrivenInteractiveTransition中的三个方法:
  分别是:(根据手势百分比调用这三个方法)
  //控制动画的百分比执行
  public func updateInteractiveTransition(percentComplete: CGFloat)
  //取消转场时调用
  public func cancelInteractiveTransition()
  //完成转场时调用
  public func finishInteractiveTransition()

你可能感兴趣的:(IOS自定义转场小记)