碰巧遇到这个需求,简单的看了下kittenyang的笔记,有些东西还是要记录一下,也方便以后研究。
1,A Push B,A要实现UINavigationControllerDelegate
2,上面的delegate中,需要实现的方法返回值是一个“转场动画”
3,转场动画是一个id<UIViewControllerAnimatedTransitioning>
4,需要实现的动画,在<UIViewControllerAnimatedTransitioning>
方法中实现
5,第二步的返回值,alloc init一个转场动画,并返回
A Push B,A要实现UINavigationControllerDelegate
这句代码务必不要忘记。
self.navigationController.delegate = self;
代理中有个方法:
- (nullable id <UIViewControllerAnimatedTransitioning>)
navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
实现了以上方法,push的过程中就会调用,返回值是一个遵循了UIViewControllerAnimatedTransitioning
代理的id值,可以理解为,返回值就是一个转场动画。通常我们可以自定义一个继承自NSObject的类并实现这个协议。
自定义转场动画
上面提到,遵循了UIViewControllerAnimatedTransitioning
代理的id值可以理解为一个转场动画,我们来定义一个这个玩意。
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MagicMoveTransition :
NSObject<UIViewControllerAnimatedTransitioning>
@end
注意import了UIKit
然后.m文件中,需要实现两个方法,
- (NSTimeInterval)transitionDuration:(nullable id < UIViewControllerContextTransitioning>)transitionContext;
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
第一个方法返回一个执行时间,第二个方法中,定义需要的动画效果。
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext{
return 0.6f;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
//获取两个VC 和 动画发生的容器
FirstCollectionViewController *fromVC = (FirstCollectionViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
SecondViewController *toVC = (SecondViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//containerView包含转换过程中涉及到的view
UIView *containerView = [transitionContext containerView];
//对Cell上的 imageView 截图,同时将这个 imageView 本身隐藏
CollectionViewCell *cell =(CollectionViewCell *)[fromVC.collectionView cellForItemAtIndexPath:[[fromVC.collectionView indexPathsForSelectedItems] firstObject]];
fromVC.indexPath = [[fromVC.collectionView indexPathsForSelectedItems]firstObject];
UIView * snapShotView = [cell.imageView snapshotViewAfterScreenUpdates:NO];
//将cell中图片的frame从他的父视图坐标系转到containerView坐标系中
snapShotView.frame = fromVC.finalCellRect = [containerView convertRect:cell.imageView.frame fromView:cell.imageView.superview];
cell.imageView.hidden = YES;
//设置第二个控制器的位置、透明度
toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
toVC.view.alpha = 0;
toVC.imageViewForSecond.hidden = YES;
//把动画前后的两个ViewController加到容器中,顺序很重要,snapShotView在上方
[containerView addSubview:toVC.view];
[containerView addSubview:snapShotView];
//动起来。第二个控制器的透明度0~1;让截图SnapShotView的位置更新到最新;
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:0.6f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveLinear animations:^{
[containerView layoutIfNeeded];
toVC.view.alpha = 1.0;
snapShotView.frame = [containerView convertRect:toVC.imageViewForSecond.frame fromView:toVC.view];
} completion:^(BOOL finished) {
//为了让回来的时候,cell上的图片显示,必须要让cell上的图片显示出来
toVC.imageViewForSecond.hidden = NO;
cell.imageView.hidden = NO;
[snapShotView removeFromSuperview];
//告诉系统动画结束
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
}];
}
很不负责的粘贴了一大段代码,具体需求具体分析吧。这样在push的controller里,代理方法就可以这样写
`
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC{
if ([toVC isKindOfClass:[SecondViewController class]]) {
MagicMoveTransition *transition = [[MagicMoveTransition alloc]init];
return transition;
}else{
return nil;
}
}
`
加一句,苹果官方规定:所有的动画视图都必须放在transitionContext的containerView里。
搞定。
FYI
http://www.kittenyang.com/uiviewcontrollertransitioning/
https://github.com/boycechang/BCMagicTransition