TransitionAnimation 学习笔记

酷炫的转场动画难的是想象,不是实现。

UIViewControllerTransitioning.h里面包含了所有你想要的

image

其中 ** UIViewControllerContextTransitioning ** 协议是转场动画上下文,看其属性就能看到,它提供了大多部分转场相关的细节内容

你可以拿到容器视图 :** containerView() -> UIView? **

还可以拿到动画的主要两个页面:** viewForKey(key: String) -> UIView? **

 // viewForKey: may return nil which would indicate that the animator should    not
// manipulate the associated view controller's view.
@available(iOS 8.0, *)
public func viewForKey(key: String) -> UIView? 
 
key:UITransitionContextFromViewKey 或者 UITransitionContextToViewKey 

在 Modal 转场里要注意,从上面可以知道,Custom 模式下,fromView 并不受 containerView 管理,这时通过viewForKey:方法来获取 fromView 得到的是 nil.但是你可以用下面的方法来搞

获取视图 ** public func viewControllerForKey(key: String) -> UIViewController? **
key:UITransitionContextToViewControllerKey, and UITransitionContextFromViewControllerKey. 再通过.view 来拿到对应的 to/from View

好了 还有常用切很重要的 ** public func completeTransition(didComplete: Bool)
**
动画执行后 必须写上这句话。 还有一个 ** transitionWasCancelled() -> Bool **

下面这些看注释就清楚了,跟交互相关的,在实现交互的时候也就这几个函数。下面细讲

public func isInteractive() -> Bool // This indicates whether the transition is currently interactive.//是否是与用户交互控制的动画

    // It only makes sense to call these from an interaction controller that
// conforms to the UIViewControllerInteractiveTransitioning protocol and was
// vended to the system by a container view controller's delegate or, in the case
// of a present or dismiss, the transitioningDelegate.
public func updateInteractiveTransition(percentComplete: CGFloat)
public func finishInteractiveTransition()
public func cancelInteractiveTransition()

实现一个简单的转场动画

1.实现对应的控制器代理方法,简单告诉他返回你已经实现了转场动画协议的类对象,或者实现了交互协议的类对象就好了。 注意如果当前不是交互控制转场的,在转场协议里面要返回nil。

push,pop转场 navi-UINavigationControllerDelegate

    @available(iOS 7.0, *)
    optional public func navigationController(navigationController: UINavigationController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
    
    @available(iOS 7.0, *)
    optional public func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

selelcted-UITabBarControllerDelegate

@available(iOS 7.0, *)
optional public func tabBarController(tabBarController: UITabBarController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

@available(iOS 7.0, *)
optional public func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

present,dismiss - transitioningDelegate

@available(iOS 2.0, *)
optional public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?

@available(iOS 2.0, *)
optional public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?

optional public func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

optional public func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

@available(iOS 8.0, *)
optional public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?

含有interactionControllerForAnimationController是交互协议,animationControllerForDismissedController是告诉代理,转场的时候要返回我自己的转场动画控制器。比如这里我返回了一个已经实现UIViewControllerAnimatedTransitioning的动画控制器

extension PushOneVC:UINavigationControllerDelegate {
    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        if operation == .Pop && fromVC == self {
            return nil
        }
        
        let type:CircleTransitionType = operation == .Push ? CircleTransitionType.NavTransitionPush :CircleTransitionType.NavTransitionPop
        
        return CirclePushAnimationController(type: type)
    }
2.实现转场动画协议里面的两个方法
class CirclePushAnimationController: NSObject,UIViewControllerAnimatedTransitioning {

//告诉他动画的时常    
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 0.5
}

//这个是最主要的,告诉他执行什么动画
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
...
1.拿到容器视图containerView,fromVC,toVC
2.将要执行动画的视图 toView 加到containerView中,其中fromView 系统已经自动加入.
** 注意,如果当前是present切style是Custom,那么在dismiss的时候就不要加toView了,你可以这样简单理解,一般在转场结束completeTransition后,会自动将fromView从容器中移除。但是Custom类型的时候却没有移除,你可以明显的看到。因此在dismiss的时候,之前的fromView 也就变成了toView。**
3.书写酷炫的动画代码,一般使用UIView 的类方法 animaitonWithDur....,如果对toView/fromView的layer做动画就用CABaseAnimation去做。
4.在completion回调或者animationdidStop中记得写上 completeTransition(ture)很重要
}

}
3.实现手势交互

在每一个需要交互控制转场动画的根图层上加一个pan手势,根据手势的状态或者拖动比例以及系统提供的updateInteractiveTransition(0.7)等方法来控制转场进度。

注意一点要在对应的交互代理方法interactionControllerForPresentation中判断当前是不是正在进行手势交互,如果是的话才返回对应的交互控制器,如果不是的话就返回nil

系统提供了一个手势交互控制器,UIPercentDrivenInteractiveTransition。当然你也可以继承他,写一个自己的手势交互控制器。记得一点,每一个需要交互控制转场动画的视图都需要一个自己的交互手势。如fromVC 和 toVC都应该有一个自己的交互手势。这也不难理解。

下面是手势交互控制转场进度的代码

    switch gesture.state {
    case .Began :
        interation = true
        self.startTransition()
    case .Changed :
        updateInteractiveTransition(percent)
    
    case .Ended :
        completionSpeed = 0.99
        interation = false
        if percent > 0.7 {
            finishInteractiveTransition()
        }else {
            cancelInteractiveTransition()
        }
    case .Cancelled :
        interation = false
        completionSpeed = 0.99
        cancelInteractiveTransition()
    default :
        break
    }
}

func startTransition() {
    switch type {
    case .Presente, .Push :
        
        if  let closure = config {
            closure()//执行nav.pushViewController()或者self.presentViewController
        }
    case .Pop:
        vc?.navigationController?.popViewControllerAnimated(true)
    case .Dismiss:
        vc?.dismissViewControllerAnimated(true, completion: nil)
    }
}

最后说一下,你可以通过使用截图snapshotViewAfterScreenUpdates:NO(YES代表立即刷新视图然后截图),对截图和toView进行动画操作,来做一些cell的转场动画。 你也可以通过layer.mask属性做最上面gif图的转场效果,对这个属性有疑问的,可以百度搜alpha通道,会帮助理解。

Demo下载地址

转场详解

另一篇参考博客

推荐阅读更多精彩内容