CABasicAnimation

1.改变transform的动画

 //旋转
 layer.transform = CATransform3DRotate(_layer.transform, 10/180.0\*M_PI, 1, 1, 1);
 //放大缩小
 layer.transform = CATransform3DScale(_layer.transform, 2, 2, 2);
 //平移
 layer.transform = CATransform3DTranslate(_layer.transform, 10, 10, 0);

2.CABasicAnimation

CALayer *Layer = [[CALayer alloc] init];
Layer.backgroundColor = [UIColor blueColor].CGColor;
Layer.frame = CGRectMake(100, 100, 50, 50);
Layer.cornerRadius = 10;
   
[self.view.layer addSublayer:scaleLayer];
   
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
scaleAnimation.toValue = [NSNumber numberWithFloat:1.5];

scaleAnimation.fillMode = kCAFillModeForwards;     //最后以什么填充
scaleAnimation.repeatCount = MAXFLOAT;    //重复次数
scaleAnimation.duration = 1;    //动画的时间

[Layer addAnimation:scaleAnimation forKey:nil];
常用的属性有
 transform
 transform.scale        //比例的转换
 transform.ratation.z   //绕着z轴旋转
 opacity                //透明度
 position               //位置的改变

3.CAAnimationGroup

    CALayer *Layer = [[CALayer alloc] init];
    Layer.backgroundColor = [UIColor blueColor].CGColor;
    Layer.frame = CGRectMake(100, 100, 50, 50);
    Layer.cornerRadius = 10;
    [self.view.layer addSublayer:Layer];
    //放大
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:1.5];
    //渐变
    CABasicAnimation * opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = @1;
    opacityAnimation.toValue = @0;
    //移动
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
    //旋转
    CABasicAnimation *rotateANimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    rotateANimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    rotateANimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(Layer.transform, M_PI, 0, 0, 1)];
    
    CAAnimationGroup * group = [CAAnimationGroup animation];
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.repeatCount = MAXFLOAT;
    group.duration = 1;
 
    group.animations =@[scaleAnimation,opacityAnimation,moveAnimation,rotateANimation];
    [Layer addAnimation:group forKey:nil];

P.S.#

再旋转中,上边的写法转换成弧度之后不能转多圈,下面实现转多圈

    CABasicAnimation *zrotateANimation = CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    zrotateANimation.fromValue = [NSNumber numberWithFloat:0];
    zrotateANimation.toValue = [NSNumber numberWithFloat:M_PI * 4.0];

你可能感兴趣的:(CABasicAnimation)