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];
} 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)