1、要实现动画效果,首先初始化CALayer
CAlayer *layer=[[CALayer alloc]init];
layer.frame=CGRectMake(20, 20, 15, 30);
[self.view.layer addSublayer:layer];
_mlayer.contents=(id)[UIImage imageNamed:@"petal"].CGImage;
2、移动layer的position
CABasicAnimation
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"position"];
初始位置
animation.fromValue=[NSValue valueWithCGPoint:layer.position];
移动后位置CGPoint *point
animation.toValue=[NSValue valueWithCGPoint:point];
3、旋转layer
CABasicAnimation *rotateAnimationY=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
初始角度
rotateAnimationY.fromValue=[NSNumber numberWithFloat:0];
旋转后位置
rotateAnimationY.toValue=[NSNumber numberWithFloat:10*M_PI];
4、组合 把之前的所有动画组合在一起
开始组合
CAAnimationGroup *group=[CAAnimationGroup animation];
layer.position=point;(这个才可以设置动画结束后的位置)
//设置播放速度
group.speed=0.1;
group.fillMode=kCAFillModeForwards;(这个的几个Mode还不知道有什么作用,哪位大神路过可以给我教教)
将需要组合的动画对象打包数组中
group.animations=[NSArray arrayWithObjects:animation,rotateAnimationY ,nil];
开始动画
[_mlayer addAnimation:group forKey:@"_mLayerMvoe"];
属性:
autoreverses设置为YES时,动画结束后动画的返回到原来的位置
duration 动画的持续时间
speed 动画速度 默认为1.0 会影响duration
repeatCount 重复次数 默认为0 播放一次