iOS 仿小紅書的 push pop轉(zhuǎn)場(chǎng)動(dòng)畫

iOS 7之后,蘋果提供了自定義轉(zhuǎn)場(chǎng)動(dòng)畫的API,我們可以自己去定義任意動(dòng)畫效果。本篇為筆者學(xué)習(xí)push、pop自定義轉(zhuǎn)場(chǎng)效果的筆記,如何有任何不正確或者有指導(dǎo)意見的,請(qǐng)?jiān)谠u(píng)論中留下您的寶貴意見!!!

本篇只講其中的UIViewControllerAnimatedTransitioning協(xié)議,來實(shí)現(xiàn)push、pop動(dòng)畫效果。另外的幾個(gè),后面會(huì)繼續(xù)學(xué)習(xí)總結(jié)!!!

我們要實(shí)現(xiàn)push、pop自定義轉(zhuǎn)場(chǎng)效果,我們必須要有一個(gè)遵守了UIViewControllerAnimatedTransitioning協(xié)議且實(shí)現(xiàn)其必須實(shí)現(xiàn)的代理方法的類。

下面我們先了解下協(xié)議:
@protocol UIViewControllerAnimatedTransitioning <NSObject>

// This is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
// synchronize with the main animation.
//
// 指定轉(zhuǎn)場(chǎng)動(dòng)畫時(shí)長(zhǎng),必須實(shí)現(xiàn),否則會(huì)Crash。
// 這個(gè)方法是為百分比驅(qū)動(dòng)的交互轉(zhuǎn)場(chǎng)和有對(duì)比動(dòng)畫效果的容器類控制器而定制的。

  • (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;

// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
// 若非百分比驅(qū)動(dòng)的交互過渡效果,這個(gè)方法只能為空

  • (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

@optional

// This is a convenience and if implemented will be invoked by the system when the transition context's completeTransition: method is invoked.

  • (void)animationEnded:(BOOL) transitionCompleted;

@end
下面是具體的實(shí)現(xiàn),
---------自定義push動(dòng)畫-----------
//轉(zhuǎn)場(chǎng)過渡的容器view
UIView *containerView = [transitionContext containerView];

//FromVC
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *fromView = fromViewController.view;
[containerView addSubview:fromView];

//ToVC
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *toView = toViewController.view;
[containerView addSubview:toView];
toView.alpha = 0;

//過渡的圖片
UIImageView *transitionImgView = [[UIImageView alloc] initWithImage:self.transitionImgView.image];
transitionImgView.frame = self.transitionBeforeImgFrame;

[transitionContext.containerView addSubview:transitionImgView];

[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
    transitionImgView.frame = self.transitionAfterImgFrame;
    toView.alpha = 1;
} completion:^(BOOL finished) {
    [transitionImgView removeFromSuperview];
    BOOL wasCancelled = [transitionContext transitionWasCancelled];
    //設(shè)置transitionContext通知系統(tǒng)動(dòng)畫執(zhí)行完畢
    [transitionContext completeTransition:!wasCancelled];
}];

------------自定義pop動(dòng)畫----------
//轉(zhuǎn)場(chǎng)過渡的容器view
UIView *containerView = [transitionContext containerView];

//ToVC
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *toView = toViewController.view;
[containerView addSubview:toView];

//圖片背景的空白view (設(shè)置和控制器的背景顏色一樣,給人一種圖片被調(diào)走的假象)
UIView *imgBgWhiteView = [[UIView alloc] initWithFrame:self.transitionBeforeImgFrame];
imgBgWhiteView.backgroundColor = [UIColor clearColor];
[containerView addSubview:imgBgWhiteView];

//有漸變的白色背景
UIView *bgView = [[UIView alloc] initWithFrame:containerView.bounds];
bgView.backgroundColor = [UIColor whiteColor];
bgView.alpha = 1;
[containerView addSubview:bgView];

//過渡的圖片
UIImageView *transitionImgView = [[UIImageView alloc] initWithImage:self.transitionImgView.image];
transitionImgView.frame = self.transitionAfterImgFrame;
transitionImgView.layer.cornerRadius = 20;
transitionImgView.alpha = 0.3;
transitionImgView.layer.masksToBounds = YES;
[transitionContext.containerView addSubview:transitionImgView];

[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
    transitionImgView.frame = self.transitionBeforeImgFrame;
    bgView.alpha = 0;
} completion:^(BOOL finished) {
    BOOL wasCancelled = [transitionContext transitionWasCancelled];
    [imgBgWhiteView removeFromSuperview];
    [bgView removeFromSuperview];
    [transitionImgView removeFromSuperview];
    
    //設(shè)置transitionContext通知系統(tǒng)動(dòng)畫執(zhí)行完畢
    [transitionContext completeTransition:!wasCancelled];
}];

分析Push動(dòng)畫

我們暫不細(xì)說UIViewControllerContextTransitioning協(xié)議,我們這里只使用到了-containerView這個(gè)代理方法,我們可以通過蘋果提供的鍵來獲取對(duì)應(yīng)的控制器:
ViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
獲取到了fromVC,也就是當(dāng)前要從哪個(gè)控制器切換。
然后通過:
DetailController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
獲取到了toVC,也就是切換到哪一個(gè)控制器。
然后再通過
UIView *containerView = [transitionContext containerView];
獲取containerView視圖。這個(gè)是一個(gè)代理方法,可以獲取到視圖容器。

下面我們獲取fromVC所點(diǎn)擊的圖片控件,然后通過-snapshotViewAfterScreenUpdates:將所點(diǎn)擊的圖片控件截圖并用于切換使用,參數(shù)設(shè)置為NO,否則動(dòng)畫會(huì)很生硬。最后,我們還要將這個(gè)所點(diǎn)擊的圖片控件的坐標(biāo)轉(zhuǎn)換成容器視圖的坐標(biāo).
UIView *toImageView = toVC.imgView;
fromImageView.hidden = YES;
toVC.view.alpha = 0.0;
toImageView.hidden = YES;

下面這兩行是非常關(guān)鍵的,并且必須保證tempView在最上層,否則動(dòng)畫效果就沒有了。先將目標(biāo)控制器的視圖添加到容器中,再添加源圖片的截圖到容器中,用于顯示切換效果。
[containerView addSubview:toVC.view];
[containerView addSubview:tempView];
我們?cè)趧?dòng)畫中,將初始的截圖的frame改變成最終的效果的frame即可達(dá)到我們的目標(biāo)效果。另外要注意還需要將坐標(biāo)轉(zhuǎn)換成容器的坐標(biāo):
tempView.frame = [toImageView convertRect:toImageView.bounds toView:containerView];
當(dāng)動(dòng)畫完成以后,一定要調(diào)用:[transitionContext completeTransition:YES],設(shè)置切換動(dòng)畫已經(jīng)完成,否則想要pop回去就不能了。
pop和push差不多,這里就不贅述了。
如需demo,請(qǐ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ù)。

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