CABasicAnimation

一、简介

  • CABasicAnimation是CAPropertyAnimation的子类, CAPropertyAnimation有一个字符串类型的keyPath属性
结构图.png
  • keyPath内容是CALayer的可动画Animatable属性,可动画属性可见CAlayer篇

  • 我们可以指定CALayer的某个属性名为keyPath,并且对CALayer的这个属性的值进行修改,达到相应的动画效果。

    • 例如:指定keyPath = @"position",就会修改CALayer的position属性的值,- > 可以实现平移的动画效果
  • 属性说明: fromValue:keyPath相应属性的初始值,ntoValue:keyPath相应属性的结束值

  • 因此,初始化好CAPropertyAnimation的子类对象后,必须先设置keyPath(修改的是CALayer的哪个属性)-> 指明执行的是怎样的动画(平移/缩放/旋转等)

动画过程说明:

  • 随着动画的进行,在长度为 duration的持续时间内,keyPath相应属性的值从fromValue渐渐地变为toValue
  • keyPath内容是CALayer的可动画Animatable属性
  • 如果fillMode=kCAFillModeForwards同时removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。

二、应用

1、缩放动画

  • 多种方案,这里实现三种就。

  • 方案一:CABasicAnimation animationWithKeyPath:@"bounds"

    • layer会从原来的尺寸变为30x30
 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];
anim.duration = 2;

anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 30, 30)];

[_myView.layer addAnimation:anim forKey:nil];
  • 方案二:CABasicAnimation animationWithKeyPath:@"transform"
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1.5; // 动画持续1.5s

// CALayer的宽度从0.5倍变为2倍
// CALayer的高度从0.5倍变为1.5倍
anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1)];
anim.toValue  = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 1.5, 1)];

[_myView.layer addAnimation:anim forKey:nil];
  • 方案三:anim.keyPath = @"transform.scale";
// 创建CABasicAnimation
    CABasicAnimation *anim = [CABasicAnimation animation];
    
    // 告诉系统修改图层的哪个属性
    
    anim.keyPath = @"transform.scale";
    
    // 告诉系统修改图层的哪个值
    //    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
    anim.toValue = @0.5;
    
    // 取消反弹
    // 告诉在动画结束的时候不要移除
    anim.removedOnCompletion = NO;
    // 始终保持最新的效果
    anim.fillMode = kCAFillModeForwards;
    
    [_redView.layer addAnimation:anim forKey:nil];

2、平移动画

  • 多种方式实现

  • 2.1方式一:CABasicAnimation -> anim.keyPath = @"position";

    • 注意:该方式动画执行完毕后,并没有真正改变CALayer的position属性的值,一切都是假象,并不会真实修改layer的属性 ,不会平移到(250,500)
    CABasicAnimation *anim = [CABasicAnimation animation];
    
    anim.keyPath = @"position";
    
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 500)];
    
    // 必须设置代理
    anim.delegate = self;
    
    // 取消反弹
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    
    [_redView.layer addAnimation:anim forKey:nil];
  • 2.2 CABasicAnimation -> animationWithKeyPath:@"transform"
    • 注意:通过CALayer的transform属性实现平移动画,layer会从自己的初始位置平移到(350, 350)位置
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1;

CATransform3D form = CATransform3DMakeTranslation(350, 350, 0);
anim.toValue = [NSValue valueWithCATransform3D:form];

[_myView.layer addAnimation:anim forKey:nil];
  • 2.3 _redView.layer.position :修改layer的可动画属性,UIView 的方法animateWithDuration:.......执行动画
    • 该方式,实现了真正的平移到(250,500)
   [UIView animateWithDuration:0.25 animations:^{
       
        _redView.layer.position = CGPointMake(250, 500);
        
    } completion:^(BOOL finished) {
        NSLog(@"%@", NSStringFromCGPoint(_redView.layer.position));
    }];

3、旋转动画

  • 这里就例举一种实现方案了。
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1.5;

// 绕着(0, 0, 1)这个向量轴 Z 轴,顺时针旋转45°(M_PI_4)
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];

[_redView.layer addAnimation:anim forKey:nil];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,562评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,141评论 5 13
  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 3,153评论 1 23
  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 3,052评论 0 21
  • 本文目录 一、平移动画 二、缩放动画 三、旋转动画 四、其他 CABasicAnimation是CAPropert...
    随风飘荡的小逗逼阅读 284评论 0 0