TransitionAnimation 學(xué)習(xí)筆記

酷炫的轉(zhuǎn)場動畫難的是想象,不是實(shí)現(xiàn)。

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

image

其中 ** UIViewControllerContextTransitioning ** 協(xié)議是轉(zhuǎn)場動畫上下文,看其屬性就能看到,它提供了大多部分轉(zhuǎn)場相關(guān)的細(xì)節(jié)內(nèi)容

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

還可以拿到動畫的主要兩個(gè)頁面:** 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 轉(zhuǎn)場里要注意,從上面可以知道,Custom 模式下,fromView 并不受 containerView 管理,這時(shí)通過viewForKey:方法來獲取 fromView 得到的是 nil.但是你可以用下面的方法來搞

獲取視圖 ** public func viewControllerForKey(key: String) -> UIViewController? **
key:UITransitionContextToViewControllerKey, and UITransitionContextFromViewControllerKey. 再通過.view 來拿到對應(yīng)的 to/from View

好了 還有常用切很重要的 ** public func completeTransition(didComplete: Bool)
**
動畫執(zhí)行后 必須寫上這句話。 還有一個(gè) ** transitionWasCancelled() -> Bool **

下面這些看注釋就清楚了,跟交互相關(guān)的,在實(shí)現(xiàn)交互的時(shí)候也就這幾個(gè)函數(shù)。下面細(xì)講

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()

實(shí)現(xiàn)一個(gè)簡單的轉(zhuǎn)場動畫

1.實(shí)現(xiàn)對應(yīng)的控制器代理方法,簡單告訴他返回你已經(jīng)實(shí)現(xiàn)了轉(zhuǎn)場動畫協(xié)議的類對象,或者實(shí)現(xiàn)了交互協(xié)議的類對象就好了。 注意如果當(dāng)前不是交互控制轉(zhuǎn)場的,在轉(zhuǎn)場協(xié)議里面要返回nil。

push,pop轉(zhuǎn)場 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是交互協(xié)議,animationControllerForDismissedController是告訴代理,轉(zhuǎn)場的時(shí)候要返回我自己的轉(zhuǎn)場動畫控制器。比如這里我返回了一個(gè)已經(jīng)實(shí)現(xiàn)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.實(shí)現(xiàn)轉(zhuǎn)場動畫協(xié)議里面的兩個(gè)方法
class CirclePushAnimationController: NSObject,UIViewControllerAnimatedTransitioning {

//告訴他動畫的時(shí)常    
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 0.5
}

//這個(gè)是最主要的,告訴他執(zhí)行什么動畫
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
...
1.拿到容器視圖containerView,fromVC,toVC
2.將要執(zhí)行動畫的視圖 toView 加到containerView中,其中fromView 系統(tǒng)已經(jīng)自動加入.
** 注意,如果當(dāng)前是present切style是Custom,那么在dismiss的時(shí)候就不要加toView了,你可以這樣簡單理解,一般在轉(zhuǎn)場結(jié)束completeTransition后,會自動將fromView從容器中移除。但是Custom類型的時(shí)候卻沒有移除,你可以明顯的看到。因此在dismiss的時(shí)候,之前的fromView 也就變成了toView。**
3.書寫酷炫的動畫代碼,一般使用UIView 的類方法 animaitonWithDur....,如果對toView/fromView的layer做動畫就用CABaseAnimation去做。
4.在completion回調(diào)或者animationdidStop中記得寫上 completeTransition(ture)很重要
}

}
3.實(shí)現(xiàn)手勢交互

在每一個(gè)需要交互控制轉(zhuǎn)場動畫的根圖層上加一個(gè)pan手勢,根據(jù)手勢的狀態(tài)或者拖動比例以及系統(tǒng)提供的updateInteractiveTransition(0.7)等方法來控制轉(zhuǎn)場進(jìn)度。

注意一點(diǎn)要在對應(yīng)的交互代理方法interactionControllerForPresentation中判斷當(dāng)前是不是正在進(jìn)行手勢交互,如果是的話才返回對應(yīng)的交互控制器,如果不是的話就返回nil

系統(tǒng)提供了一個(gè)手勢交互控制器,UIPercentDrivenInteractiveTransition。當(dāng)然你也可以繼承他,寫一個(gè)自己的手勢交互控制器。記得一點(diǎn),每一個(gè)需要交互控制轉(zhuǎn)場動畫的視圖都需要一個(gè)自己的交互手勢。如fromVC 和 toVC都應(yīng)該有一個(gè)自己的交互手勢。這也不難理解。

下面是手勢交互控制轉(zhuǎn)場進(jìn)度的代碼

    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()//執(zhí)行nav.pushViewController()或者self.presentViewController
        }
    case .Pop:
        vc?.navigationController?.popViewControllerAnimated(true)
    case .Dismiss:
        vc?.dismissViewControllerAnimated(true, completion: nil)
    }
}

最后說一下,你可以通過使用截圖snapshotViewAfterScreenUpdates:NO(YES代表立即刷新視圖然后截圖),對截圖和toView進(jìn)行動畫操作,來做一些cell的轉(zhuǎn)場動畫。 你也可以通過layer.mask屬性做最上面gif圖的轉(zhuǎn)場效果,對這個(gè)屬性有疑問的,可以百度搜a(bǔ)lpha通道,會幫助理解。

Demo下載地址

轉(zhuǎn)場詳解

另一篇參考博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容