Model:
AViewController -> BViewController 从A到B
需要设置BViewController的transitioningDelegate
来控制动画,通过modalPresentationStyle
来控制动画类型.
其中transitioningDelegate
需要实现UIViewControllerTransitioningDelegate
协议.
UIViewControllerTransitioningDelegate
:
-
如果只需实现动画无手势操作,只需实现下面
Presented
和Dismissed
两个方法.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()