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

版本:Xcode 7.0.1
语言:Objective-C

转场动画就是viewController之间切换的动画。
主要有以下三种自定义方法:

  • 列Push & Pop
  • Modal
  • Segue

第一种是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。

  1. 在SecondViewController也创建一个ImageView
@property (nonatomic, retain) UIImageView * avatarImageView; // 替身imageView,在toVC中

开始动画

  1. 创建一个文件继承自 NSObject(我把它取名叫PushTransition), 并在<strong>.h</strong>文件中遵守UIViewControllerAnimatedTransitioning协议。
    由于Push和Pop是两套动画,所以需要两个这样的文件,判断动画类型的办法在下面讨论。
  2. 实现协议的两个方法
// 指定动画的持续时长
 1. (NSTimeInterval)transitionDuration;
// 转场动画的具体内容
 2. (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
  1. 在其中编写 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,在后面的动画中慢慢显示出来变为1
    toVC.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中

  1. 在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的转场动画就完成了

点击此处下载源码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 这篇文章,我将讲述几种转场动画的自定义方式,并且每种方式附上一个示例,毕竟代码才是我们的语言,这样比较容易上...
    伯恩的遗产阅读 54,254评论 37 381
  • OC开发我们主要有以下三种自定义方法,供大家参考:Push & PopModalSegue 前两种大家都很熟悉,第...
    ScaryMonsterLyn阅读 5,616评论 1 3
  • 概述 这篇文章,我将讲述几种转场动画的自定义方式,并且每种方式附上一个示例,毕竟代码才是我们的语言,这样比较容易上...
    iOS_Developer阅读 4,645评论 0 4
  • 准备: 苹果在iOS7之后,提供了自定义转场API。使得我们可以对模态(present、dismiss)、导航控制...
    M_慕宸阅读 5,317评论 0 7
  • 写出我心写出我心写出我心重复的事情说三遍,《写出我心》这本书让我养成了写作的好习惯,在作者的鼓励下,我成功的写下了...
    南瓜橙长阅读 2,490评论 0 0

友情链接更多精彩内容