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

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

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

image

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

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

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

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

好了 還有常用切很重要的 ** public func completeTransition(didComplete: Bool)
**
動(dòng)畫執(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.//是否是與用戶交互控制的動(dòng)畫

    // 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è)簡(jiǎn)單的轉(zhuǎn)場(chǎng)動(dòng)畫

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

push,pop轉(zhuǎn)場(chǎng) 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)場(chǎng)的時(shí)候要返回我自己的轉(zhuǎn)場(chǎng)動(dòng)畫控制器。比如這里我返回了一個(gè)已經(jīng)實(shí)現(xiàn)UIViewControllerAnimatedTransitioning的動(dòng)畫控制器

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)場(chǎng)動(dòng)畫協(xié)議里面的兩個(gè)方法
class CirclePushAnimationController: NSObject,UIViewControllerAnimatedTransitioning {

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

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

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

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

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

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

下面是手勢(shì)交互控制轉(zhuǎn)場(chǎng)進(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代表立即刷新視圖然后截圖),對(duì)截圖和toView進(jìn)行動(dòng)畫操作,來做一些cell的轉(zhuǎn)場(chǎng)動(dòng)畫。 你也可以通過layer.mask屬性做最上面gif圖的轉(zhuǎn)場(chǎng)效果,對(duì)這個(gè)屬性有疑問的,可以百度搜a(bǔ)lpha通道,會(huì)幫助理解。

Demo下載地址

轉(zhuǎn)場(chǎng)詳解

另一篇參考博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,001評(píng)論 6 537
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,786評(píng)論 3 423
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,986評(píng)論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,204評(píng)論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,964評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,354評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,410評(píng)論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,554評(píng)論 0 289
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,106評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,918評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,093評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,648評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,342評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,755評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,009評(píng)論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,839評(píng)論 3 395
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,107評(píng)論 2 375

推薦閱讀更多精彩內(nèi)容