iOS转场动画的三种方式

本文介绍了在iOS中实现转场动画的三种方式:1) CATransition,它是CAAnimation子类,通过设置type和subtype属性实现不同类型的动画效果;2) transitionFromViewController,用于在父视图控制器的子视图控制器之间切换;3) 自定义转场动画,包括UINavigationControllerDelegate结合UIViewControllerAnimatedTransitioning以及UIViewControllerTransitioningDelegate结合UIViewControllerAnimatedTransitioning,详细讲解了如何创建和实现相关动画效果。
摘要由CSDN通过智能技术生成

1.CATransition

CATransition是CAAnimation的子类,用于过渡动画或转场动画。为视图层移入移除屏幕提供转场动画。首先来看一下简单的Demo:

   CATransition *animation = [CATransition animation];    animation.type = kCATransitionFade;    animation.subtype = kCATransitionFromRight;    animation.duration = 1.0;    // 在window上执行CATransition, 即可在ViewController转场时执行动画    [self.view.window.layer addAnimation:animation forKey:@"kTransitionAnimation"];        AViewController *vc = [[AViewController alloc] init];    [self presentViewController:vc animated:NO completion:nil];

将该动画添加到window.layer上,则会present或push时使用指定的转场动画。
其中最主要的两个属性就是type和subtype。

  • type:转场动画的类型。
    官方SDK只提供了四种转场动画的类型,即:

CA_EXTERN NSString * const kCATransitionFade;CA_EXTERN NSString * const kCATransitionMoveIn;CA_EXTERN NSString * const kCATransitionPush;CA_EXTERN NSString * const kCATransitionReveal;

私有的type:

NSString *const kCATransitionCube = @"cube"; NSString *const kCATransitionSuckEffect = @"suckEffect"; NSString *const kCATransitionOglFlip = @"oglFlip"; NSString *const kCATransitionRippleEffect = @"rippleEffect"; NSString *const kCATransitionPageCurl = @"pageCurl"; NSString *const kCATransitionPageUnCurl = @"pageUnCurl"; NSString *const kCATransitionCameraIrisHollowOpen = @"cameraIrisHollowOpen";NSString *const kCATransitionCameraIrisHollowClose = @"cameraIrisHollowClose";
  • subtype:动画类型的方向

CA_EXTERN NSString * const kCATransitionFromRight;CA_EXTERN NSString * const kCATransitionFromLeft;CA_EXTERN NSString * const kCATransitionFromTop;CA_EXTERN NSString * const kCATransitionFromBottom;

上面讲的是给window.layer添加transition,这样使得在present或push时使用指定的转场动画。
既然讲到这里了,就看一下把transition加在layer上。
看一下示例代码:

- (void)viewDidLoad {    [super viewDidLoad];        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];        [button setTitle:@"进入" forState:UIControlStateNormal];        button.backgroundColor = [UIColor redColor];        [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];        [self.view addSubview:button];        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 150, 150)];    [self.view addSubview:_imageView];    _imageView.backgroundColor = [UIColor redColor];    _imageArray = @[[UIImage imageNamed:@"成果秀1"],[UIImage imageNamed:@"点赞他人1"],[UIImage imageNamed:@"偷师学艺1"],[UIImage imageNamed:@"学会欣赏3"]];        _imageView.image = [UIImage imageNamed:@"成果秀1"];}- (void)buttonClicked{      CATransition *animation = [CATransition animation];    animation.type = @"cube";    animation.subtype = kCATransitionFromRight;    animation.duration = 1.0;    //换图片的时候使用转场动画    [self.imageView.layer addAnimation:animation forKey:nil];    //cycle to next image    UIImage *currentImage = self.imageView.image;    NSUInteger index = [self.imageArray indexOfObject:currentImage];    index = (index + 1) % [self.imageArray count];    self.imageView.image = self.imageArray[index];   }

2.trans

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值