『ios』轉(zhuǎn)場動畫篇一 push pop轉(zhuǎn)場實踐

1536737739532.jpg

QQ20180912-163517-HD.gif
[viewB convertRect:viewC.frame toView:viewA];
viewA是目標召嘶,viewC是被操作的對象,那么剩下的viewB自然而然就是源了做盅。作用就是計算viewB上的viewC相對于viewA的frame城舞。
[viewC convertRect:viewB.frame fromView:viewA];
該例子viewA是源抖坪,viewB是被操作的對象亿柑,那么viewC就是目標县昂。作用就是計算viewA上的viewB相對于viewC的frame肮柜。

先從文檔來

UIViewControllerContextTransitioning
UIViewControllerAnimatedTransitioning

自定義簡單轉(zhuǎn)場動畫上面兩個protocol結(jié)合下面這兩個方法就可以搞.

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

還需要注意navigation的代理

UINavigationControllerDelegate

下面這個代理方法返回的是 就是上面UIViewControllerAnimatedTransitioning.

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

為了完成動畫還需要了解一個函數(shù)

- (nullable UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);

這個函數(shù)是獲取view或者什么什么控件上的,截屏倒彰。afterUpdates是否全顯示之后再截屏审洞。

下面要開始了。
從第一步開始待讳。
遵守代理

UINavigationControllerDelegate
 self.navigationController.delegate = self;
#pragma mark -- UINavigationControllerDelegate

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    
    if (operation == UINavigationControllerOperationPush) {
        return [customNavgationPushTransition new];//如果是push的話用我們定義的那個對象push對象
    } else if (operation == UINavigationControllerOperationPop && fromVC != self) {
        return [customNavgationPopTransition new];![QQ20180912-163517-HD.gif](https://upload-images.jianshu.io/upload_images/2440780-5e5411e05137385e.gif?imageMogr2/auto-orient/strip)

    } else {
        return nil;
    }
}

如果沒有這一步芒澜,自定義的專場動畫也就不會生效。因為沒有返回UIViewControllerAnimatedTransitioning创淡。

第二步痴晦,自定義push動畫

//動畫時長
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.5;
}

//
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    //1.獲取動畫的源控制器和目標控制器
    ViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    DetailViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *container = [transitionContext containerView];
    
    //2.創(chuàng)建一個   imageView 的截圖,并把 imageView 隱藏琳彩,造成使用戶以為移動的就是 imageView 的假象
    UIView *snapshotView = [fromVC.ImageView snapshotViewAfterScreenUpdates:NO];
    //計算fromVC.view上的fromVC.ImageView.frame相對于container的坐標
    snapshotView.frame = [container convertRect:fromVC.ImageView.frame fromView:fromVC.view];
//    fromVC.selectedCell.imageView.hidden = YES;
    
    //3.設(shè)置目標控制器的位置誊酌,并把透明度設(shè)為0部凑,在后面的動畫中慢慢顯示出來變?yōu)?
    toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
    toVC.view.alpha = 0;
    toVC.bgImageView.hidden = YES;
    
    //4.都添加到 container 中。注意順序不能錯了
    [container addSubview:toVC.view];
    [container addSubview:snapshotView];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        snapshotView.frame = [container convertRect:toVC.bgImageView.frame fromView:toVC.view];
        fromVC.view.alpha = 0;
        toVC.view.alpha = 1;
    } completion:^(BOOL finished) {
        fromVC.ImageView.hidden = NO;
        toVC.bgImageView.hidden = NO;
        [snapshotView removeFromSuperview];
        
        //一定要記得動畫完成后執(zhí)行此方法术辐,讓系統(tǒng)管理 navigation 如果設(shè)置為no砚尽。可以自己試試
        [transitionContext completeTransition:YES];
    }];
    
}

完成這一步那么push的時候就會有動畫了辉词。

第三步設(shè)置pop動畫必孤,跟push大同小異。

//動畫時長
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.5;
}

//
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    //1.獲取動畫的源控制器和目標控制器
    DetailViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    ViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *container = [transitionContext containerView];
    
    //2.創(chuàng)建一個 Cell 中 imageView 的截圖瑞躺,并把 imageView 隱藏敷搪,造成使用戶以為移動的就是 imageView 的假象
    UIView *snapshotView = [fromVC.bgImageView snapshotViewAfterScreenUpdates:NO];
    snapshotView.frame = [container convertRect:fromVC.bgImageView.frame fromView:fromVC.view];
    fromVC.bgImageView.hidden = YES;
    
    //3.設(shè)置目標控制器的位置,并把透明度設(shè)為0幢哨,在后面的動畫中慢慢顯示出來變?yōu)?
    toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
    toVC.view.alpha = 0;
//    toVC..hidden = YES;
    
    //4.都添加到 container 中赡勘。注意順序不能錯了
    [container addSubview:toVC.view];
    [container addSubview:snapshotView];
    
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        snapshotView.frame = [container convertRect:toVC.ImageView.frame fromView:toVC.view];
        fromVC.view.alpha = 0;
        toVC.view.alpha = 1;
    } completion:^(BOOL finished) {
        fromVC.bgImageView.hidden = NO;
//        toVC.selectedCell.hidden = NO;
        [snapshotView removeFromSuperview];
        
        //一定要記得動畫完成后執(zhí)行此方法,讓系統(tǒng)管理 navigation
        [transitionContext completeTransition:YES];
    }];
    
}

demo地址(https://github.com/Butteryflyyer/XH_CustomTransitionl)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末捞镰,一起剝皮案震驚了整個濱河市闸与,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌岸售,老刑警劉巖践樱,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異凸丸,居然都是意外死亡拷邢,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進店門屎慢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瞭稼,“玉大人,你說我怎么就攤上這事腻惠』分猓” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵集灌,是天一觀的道長悔雹。 經(jīng)常有香客問我,道長绝页,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任寂恬,我火速辦了婚禮续誉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘初肉。我一直安慰自己酷鸦,他們只是感情好,可當我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著臼隔,像睡著了一般嘹裂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上摔握,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天寄狼,我揣著相機與錄音,去河邊找鬼氨淌。 笑死泊愧,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的盛正。 我是一名探鬼主播删咱,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼豪筝!你這毒婦竟也來了痰滋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤续崖,失蹤者是張志新(化名)和其女友劉穎敲街,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體袜刷,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡聪富,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了著蟹。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片墩蔓。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖萧豆,靈堂內(nèi)的尸體忽然破棺而出奸披,到底是詐尸還是另有隱情,我是刑警寧澤涮雷,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布阵面,位于F島的核電站,受9級特大地震影響洪鸭,放射性物質(zhì)發(fā)生泄漏样刷。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一览爵、第九天 我趴在偏房一處隱蔽的房頂上張望置鼻。 院中可真熱鬧,春花似錦蜓竹、人聲如沸箕母。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嘶是。三九已至钙勃,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間聂喇,已是汗流浹背辖源。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留授帕,地道東北人同木。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像跛十,于是被迫代替她去往敵國和親彤路。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,055評論 2 355

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