CABasicAnimation

作者: iOS_成才录 | 来源:发表于2015-11-12 19:25 被阅读4439次

一、简介

  • 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];

相关文章

网友评论

    本文标题:CABasicAnimation

    本文链接:https://www.haomeiwen.com/subject/zmkfhttx.html