iOS動(dòng)畫之-自定義轉(zhuǎn)場動(dòng)畫

自定義轉(zhuǎn)場動(dòng)畫

在iOS7之后,UIViewController之間進(jìn)行跳轉(zhuǎn)時(shí)地啰,可以對push,pop,present,dismiss添加自定義動(dòng)畫,實(shí)現(xiàn)很炫的跳轉(zhuǎn)效果。本文主要對轉(zhuǎn)場動(dòng)畫的使用方式進(jìn)行總結(jié)傲绣,實(shí)現(xiàn)自定義的轉(zhuǎn)場動(dòng)畫,主要是運(yùn)用到了以下幾個(gè)協(xié)議:

UIViewControllerAnimatedTransitioning

最關(guān)鍵的一個(gè)協(xié)議巩踏,用于實(shí)現(xiàn)自定義動(dòng)畫秃诵,主要包括以下兩個(gè)方法:

//定義動(dòng)畫的執(zhí)行時(shí)間
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
//添加自定義動(dòng)畫
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

通過實(shí)現(xiàn)animateTransition協(xié)議來實(shí)現(xiàn)動(dòng)畫的具體實(shí)現(xiàn),通過transitionContext可以獲取到當(dāng)前的viewController和將要跳轉(zhuǎn)的viewController,通過獲取到對應(yīng)viewControllerview可以用之前介紹的基礎(chǔ)動(dòng)畫來添加相應(yīng)的動(dòng)畫來實(shí)現(xiàn)轉(zhuǎn)場的動(dòng)畫塞琼,以下代碼是實(shí)現(xiàn)跳轉(zhuǎn)時(shí)進(jìn)行縮放的動(dòng)畫:

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    return 0.5;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    UIView* toView = nil;
    UIView* fromView = nil;
    UIView* transView = nil;
    
    if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
        fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    } else {
        fromView = fromViewController.view;
        toView = toViewController.view;
    }
    
    transView = toView;
    [[transitionContext containerView] addSubview:toView];
    
     transView.transform = CGAffineTransformMakeScale(0, 0);
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
         transView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
         [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
    }];
}

UIViewControllerTransitioningDelegate

UIViewController在執(zhí)行present,dismiss時(shí)菠净,通過實(shí)現(xiàn) UIViewControllerTransitioningDelegate來添加自定義動(dòng)畫,UIViewControllerTransitioningDelegate包括以下4個(gè)方法:

//指定present動(dòng)畫
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
//指定dismiss動(dòng)畫
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
//指定交互式present動(dòng)畫的控制類
- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator;
//指定交互式dismiss動(dòng)畫的控制類
- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator;

UINavigationControllerDelegate

UIViewController在執(zhí)行push,pop時(shí),通過實(shí)現(xiàn) UINavigationControllerDelegate來添加自定義動(dòng)畫嗤练,通過以下方法來具體的動(dòng)畫:

- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC 

具體的代碼可以在demo中查看。自定義轉(zhuǎn)場動(dòng)畫的代碼在CATransition目錄下在讶。
個(gè)人博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末煞抬,一起剝皮案震驚了整個(gè)濱河市榕栏,隨后出現(xiàn)的幾起案子读跷,更是在濱河造成了極大的恐慌,老刑警劉巖莲镣,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件曙强,死亡現(xiàn)場離奇詭異残拐,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)碟嘴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進(jìn)店門溪食,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人娜扇,你說我怎么就攤上這事错沃。” “怎么了雀瓢?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵枢析,是天一觀的道長。 經(jīng)常有香客問我刃麸,道長醒叁,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任泊业,我火速辦了婚禮把沼,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吁伺。我一直安慰自己智政,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布箱蝠。 她就那樣靜靜地躺著续捂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪宦搬。 梳的紋絲不亂的頭發(fā)上牙瓢,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天,我揣著相機(jī)與錄音间校,去河邊找鬼矾克。 笑死,一個(gè)胖子當(dāng)著我的面吹牛憔足,可吹牛的內(nèi)容都是我干的胁附。 我是一名探鬼主播酒繁,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼控妻!你這毒婦竟也來了州袒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤弓候,失蹤者是張志新(化名)和其女友劉穎郎哭,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體菇存,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡夸研,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了依鸥。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片亥至。...
    茶點(diǎn)故事閱讀 39,965評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖贱迟,靈堂內(nèi)的尸體忽然破棺而出抬闯,到底是詐尸還是另有隱情,我是刑警寧澤关筒,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布溶握,位于F島的核電站,受9級(jí)特大地震影響蒸播,放射性物質(zhì)發(fā)生泄漏睡榆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一袍榆、第九天 我趴在偏房一處隱蔽的房頂上張望胀屿。 院中可真熱鬧,春花似錦包雀、人聲如沸宿崭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽葡兑。三九已至,卻和暖如春赞草,著一層夾襖步出監(jiān)牢的瞬間讹堤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工厨疙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留洲守,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像梗醇,于是被迫代替她去往敵國和親知允。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評論 2 355