1. 缩放
CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"transform"];
//x,y,z放大缩小倍数
CATransform3D transform=CATransform3DMakeScale(0.5, 0.5, 1.0);
NSValue *value=[NSValue valueWithCATransform3D:transform];
[theAnimation setToValue:value];
transform=CATransform3DMakeScale(1.0, 1.0, 1.0);
value=[NSValue valueWithCATransform3D:transform];
[theAnimation setAutoreverses:YES]; //原路返回的动画一遍
[theAnimation setDuration:1.0];//执行动画的时间
[theAnimation setRepeatCount:2];//执行动画的次数
[layer addAnimation:theAnimation forKey:nil];
2.动画旋转
CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D transform=CATransform3DMakeRotation(90*M_PI/180, 1, 1, 0);//
NSValue *value = [NSValue valueWithCATransform3D:transform];
[theAnimation setToValue:value];
/*
angle:旋转的弧度,所以要把角度转换成弧度:角度 * M_PI / 180。
x:向X轴方向旋转。值范围-1 --- 1之间
y:向Y轴方向旋转。值范围-1 --- 1之间
z:向Z轴方向旋转。值范围-1 --- 1之间
向 X轴,Y轴都旋转60度,就是沿着对角线旋转。
*/
transform = CATransform3DMakeRotation(1, 1, 1, 0);
value = [NSValue valueWithCATransform3D:transform];
[theAnimation setFromValue:value];
theAnimation.duration=2;
theAnimation.autoreverses=YES;
[layer addAnimation:theAnimation forKey:nil];
3.组动画
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D rotateTransform = CATransform3DMakeRotation(1.57, 0, 0, -1);
CATransform3D scaleTransform = CATransform3DMakeScale(2, 2, 2);
//(CGFloat tx,CGFloat ty, CGFloat tz ,x,y,z轴的偏移量
CATransform3D positionTransform = CATransform3DMakeTranslation(1, 200, 0); //位置移动
CATransform3D combinedTransform =CATransform3DConcat(rotateTransform, scaleTransform); //Concat就是combine的意思
combinedTransform = CATransform3DConcat(combinedTransform, positionTransform); //再combine一次把三个动作连起来
[anim setFromValue:[NSValue valueWithCATransform3D:CATransform3DIdentity]]; //放在3D坐标系中最正的位置
[anim setToValue:[NSValue valueWithCATransform3D:combinedTransform]];
[anim setDuration:5.0f];
[layer addAnimation:anim forKey:nil];
[layer setTransform:combinedTransform]; //如果没有这句,layer执行完动画又会返回最初的state