iOS 7之后芹壕,蘋果提供了自定義轉(zhuǎn)場動畫的API窟绷,我們可以自己去定義任意動畫效果锯玛。本篇為筆者學(xué)習(xí)push、pop自定義轉(zhuǎn)場效果的筆記钾麸,如何有任何不正確或者有指導(dǎo)意見的更振,請?jiān)谠u論中留下您的寶貴意見!7钩ⅰ肯腕!
本篇只講其中的UIViewControllerAnimatedTransitioning協(xié)議,來實(shí)現(xiàn)push钥平、pop動畫效果实撒。另外的幾個(gè),后面會繼續(xù)學(xué)習(xí)總結(jié)!!!
我們要實(shí)現(xiàn)push涉瘾、pop自定義轉(zhuǎn)場效果知态,我們必須要有一個(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)場動畫時(shí)長立叛,必須實(shí)現(xiàn)负敏,否則會Crash。
// 這個(gè)方法是為百分比驅(qū)動的交互轉(zhuǎn)場和有對比動畫效果的容器類控制器而定制的秘蛇。
- (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ū)動的交互過渡效果其做,這個(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動畫-----------
//轉(zhuǎn)場過渡的容器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)動畫執(zhí)行完畢
[transitionContext completeTransition:!wasCancelled];
}];
------------自定義pop動畫----------
//轉(zhuǎn)場過渡的容器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)動畫執(zhí)行完畢
[transitionContext completeTransition:!wasCancelled];
}];
分析Push動畫
我們暫不細(xì)說UIViewControllerContextTransitioning協(xié)議妖泄,我們這里只使用到了-containerView這個(gè)代理方法,我們可以通過蘋果提供的鍵來獲取對應(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,否則動畫會很生硬璧坟。最后,我們還要將這個(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在最上層雀鹃,否則動畫效果就沒有了。先將目標(biāo)控制器的視圖添加到容器中励两,再添加源圖片的截圖到容器中黎茎,用于顯示切換效果。
[containerView addSubview:toVC.view];
[containerView addSubview:tempView];
我們在動畫中当悔,將初始的截圖的frame改變成最終的效果的frame即可達(dá)到我們的目標(biāo)效果傅瞻。另外要注意還需要將坐標(biāo)轉(zhuǎn)換成容器的坐標(biāo):
tempView.frame = [toImageView convertRect:toImageView.bounds toView:containerView];
當(dāng)動畫完成以后,一定要調(diào)用:[transitionContext completeTransition:YES]盲憎,設(shè)置切換動畫已經(jīng)完成嗅骄,否則想要pop回去就不能了。
pop和push差不多饼疙,這里就不贅述了溺森。
如需demo,請留言窑眯。