实现效果
系统自带的viewControll切换动画有push动画和模态动画,一般在正常情况下已经够用了,但并满足不了一些复杂的切换效果,比如下图一些效果github代码链接:
基本介绍
相关的内容都定义在UIKit的UIViewControllerTransitioning.h中
- 设置动画时间
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return animationDuration
}
- 在这里实现自定义动画
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
//动画之前的视图
self.fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
//即将出现的视图
self.toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)//containerView是容器的view,需要将要出现的view加载上去
transitionContext.containerView()?.addSubview(self.toVC!.view)
//添加手势到即将出现的view上
self.prepareGestureRecognizerInView(toVC!.view)
//对动画之前的视图进行调整,比如frame
self.pushBefore()
//自定义动画效果
UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.popAnimation()
}
}) { (isCompelete) -> Void in
if self.isPush
{
//完成时调用
transitionContext.completeTransition(true)
}else{
transitionContext.completeTransition(!transitionContext.transitionWasCancelled());
}
}
}
需要实现的代理方法
//返回Presented动画 func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { self.animation?.isPush = true return self.animation } //返回Dismissed动画 func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { self.animation?.isPush = false return self.animation } //返回push和pop动画 func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { if operation == UINavigationControllerOperation.Push{ self.animation!.isPush = true }else{ self.animation!.isPush = false } return self.animation }
具体实现请看代码