1.CABasicAnimation基础动画
//1.创建一个动画对象
CABasicAnimation *animation = [CABasicAnimation animation];
//2.设置属性值
// animation.keyPath = @"position.x";
animation.keyPath = @"transform.scale";
animation.toValue = @0;
//执行次数
animation.repeatCount = MAXFLOAT;
//执行时长
animation.duration = 3;
//自动反转
animation.autoreverses = YES;
//动画完成时会自动删除动画
// animation.removedOnCompletion = NO;
// animation.fillMode = @"forwards";
//3.添加动画
[self.redView.layer addAnimation:animation forKey:nil];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"transform.rotation";
//values设置多个值
animation.values = @[@10, @20, @10];
animation.repeatCount = MAXFLOAT;
animation.autoreverses = YES;
[self.redView.layer addAnimation:animation forKey:nil];
2.2CAKeyframeAnimation(帧动画)通过path设置多个值
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"transform.rotation";
//根据路径做动画
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(300, 50)];
[path addLineToPoint:CGPointMake(300, 400)];
animation.keyPath = @"position";
animation.path = path.CGPath;
animation.repeatCount = MAXFLOAT;
animation.autoreverses = YES;
[self.redView.layer addAnimation:animation forKey:nil];
3.CATransition(转场动画)根据type设置不同效果
CATransition *animation = [CATransition animation];
animation.duration = 1;
//fade', `moveIn', `push' and `reveal'. Defaults to `fade
// cube-立方体 ,suckEffect-吸走的效果,oglFlip-前后翻转效果rippleEffect-波纹效果
//pageCurl-翻页 pageUnCurl-翻页 cameraIrisHollowOpen-镜头开 cameraIrisHollowClose 镜头开关
animation.type = @"cube";
_redView.backgroundColor = [UIColor redColor];
[_redView.layer addAnimation:animation forKey:nil];
4.CABasicAnimation(动画组)
CABasicAnimation *animation1 = [CABasicAnimation animation];
animation1.keyPath = @"position.y";
animation1.toValue = @400;
CABasicAnimation *animation2 = [CABasicAnimation animation];
animation2.keyPath = @"transform.scale";
animation2.toValue = @0.5;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[animation1, animation2];
[_redView.layer addAnimation:group forKey:nil];