失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > iOS自定义转场动画(1)——自定义Push转场动画

iOS自定义转场动画(1)——自定义Push转场动画

时间:2021-02-18 15:17:56

相关推荐

iOS自定义转场动画(1)——自定义Push转场动画

版本:Xcode 7.0.1

语言:Objective-C

转场动画就是viewController之间切换的动画。

主要有以下三种自定义方法:

列Push & PopModalSegue

第一种是UINavigationController的转场方法

第二种是模态跳转

第三种是 Stroyboard 中的拖线,属于 UIStoryboardSegue 类,通过继承这个类来自定义转场过程动画。

先来看一下效果:

Push

首先说一下 Push 这种转场的自定义,操作步骤如下:

准备工作

1、新建工程,删掉Main storyboard,把程序设置的General的Main Interface清空。

2、 新建一个类继承UIViewController,在AppDelegate中创建一个UINavigationController,设置root为ViewController。

3、在ViewController创建一个ImageView,放在.h中

@property (nonatomic, retain) UIImageView * sourceImageView; // 源imageView,在fromVC中

给ImageView添加一张图片

再创建一个按钮,设置点击按钮进入SecondViewController。

4、在SecondViewController也创建一个ImageView

@property (nonatomic, retain) UIImageView * avatarImageView; // 替身imageView,在toVC中

开始写动画

5、 创建一个文件继承自 NSObject(我把它取名叫PushTransition), 并在.h文件中遵守UIViewControllerAnimatedTransitioning协议。

由于Push和Pop是两套动画,所以需要两个这样的文件,判断动画类型的办法在下面讨论。

6、实现协议的两个方法

// 指定动画的持续时长1. (NSTimeInterval)transitionDuration;// 转场动画的具体内容2. (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

7、在其中编写 Push 的动画。 具体的动画实现过程都在代码的注释里 :

// 指定动画的持续时长- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{return 0.5;}// 转场动画的具体内容- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{// 获取动画的源控制器和目标控制器ViewController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];SecondViewController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];UIView * container = transitionContext.containerView;// 创建一个imageView的截图,并把原本imageView隐藏,造成以为移动的就是imageView的假象UIView * snapshotView = [fromVC.sourceImageView snapshotViewAfterScreenUpdates:NO];snapshotView.frame = [container convertRect:fromVC.sourceImageView.frame fromView:fromVC.view];fromVC.sourceImageView.hidden = YES;// 设置目标控制器的位置,并把透明度设为0,在后面的动画中慢慢显示出来变为1toVC.view.frame = [transitionContext finalFrameForViewController:toVC];toVC.view.alpha = 0;// 都添加到container中。注意顺序[container addSubview:toVC.view];[container addSubview:snapshotView];// 执行动画[UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{snapshotView.frame = toVC.avatarImageView.frame;toVC.view.alpha = 1;} completion:^(BOOL finished) {fromVC.sourceImageView.hidden = NO;toVC.avatarImageView.image = fromVC.sourceImageView.image;[snapshotView removeFromSuperview];//一定要记得动画完成后执行此方法,让系统管理 navigation[transitionContext completeTransition:YES];}];}

使用动画

回到ViewController中

8、 在ViewController.h中遵守UINavigationControllerDelegate协议,并设置为self

// 必须在viewDidAppear或者viewWillAppear中写,因为每次都需要将delegate设为当前界面- (void)viewDidAppear:(BOOL)animated{[super viewDidAppear:animated];self.navigationController.delegate = self;}

9、实现 UINavigationControllerDelegate 协议方法:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{if (operation == UINavigationControllerOperationPush){ // 就是在这里判断是哪种动画类型return [[PushTransition alloc] init]; // 返回push动画的类}else{return nil;}}

至此,push的转场动画就完成了

如果觉得《iOS自定义转场动画(1)——自定义Push转场动画》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。