CABasicAnimation来做心跳动画

CABasicAnimation *anim = [CABasicAnimation animation];
    
    anim.keyPath = @"transform.scale";
    anim.toValue = @0.5;
    anim.repeatCount = MAXFLOAT;
    
    [_heartImageV.layer addAnimation:anim forKey:nil];//_heartImageV 上放一张🧡形图片
 

 

简单讲就是利用核心动画,设置核心动画对象的keyPath为tramsform.scale,然后将这个核心动画对象添加到图层上。

百度了一下,keyPath值还挺多的:

animationWithKeyPath的值:

  transform.scale = 比例轉換

    transform.scale.x = 闊的比例轉換

    transform.scale.y = 高的比例轉換

    transform.rotation.z = 平面圖的旋轉

    opacity = 透明度

    margin

    zPosition

    backgroundColor    背景颜色

    cornerRadius    圆角

    borderWidth

    bounds

    contents

    contentsRect

    cornerRadius

    frame

    hidden

    mask

    masksToBounds

    opacity

    position

    shadowColor

    shadowOffset

    shadowOpacity

    shadowRadius

可以分为三类吧:

1. 缩放 keypath 用bounds 或者 transform 或者  transform.scale

方案一:

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];

anim.duration = 2;

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

[_myView.layer addAnimation:anim forKey:nil];
 

方案二:(还是一比较简单)

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

方案三:(还是一比较简单)
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.平移  keypath 用position 或transform

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

3.旋转  keypath 用 transform

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

 

posted @ 2017-09-15 12:18  liuw_flexi  阅读(403)  评论(0编辑  收藏  举报