呼吸效果
比如想实现一个图片的放大缩小的呼吸效果
放大的动画时长是 2s、缩小的动画时长是 1s
代码处理
采用 CAAnimationGroup 方式实现,但是需要注意起始时间 beginTime 的参数设置
代码块
// 创建动画组
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = 3; /// 子动画的总时长
animationGroup.repeatCount = MAXFLOAT; /// 循环次数
animationGroup.removedOnCompletion = YES;
animationGroup.fillMode = kCAFillModeForwards;
// animationGroup.autoreverses = YES; 自动反向执行动画,不能打开否则会异常
// 放大
CABasicAnimation *scaleUpAnimation = [CABasicAnimation animation];
scaleUpAnimation.keyPath = @"transform.scale";
scaleUpAnimation.duration = 2;
scaleUpAnimation.fromValue = @1.0;
scaleUpAnimation.toValue = @3;
// 缩小
CABasicAnimation *scaleDownAnimation = [CABasicAnimation animation];
scaleDownAnimation.keyPath = @"transform.scale";
scaleDownAnimation.beginTime = 2; /// 从第 2s 开始执行缩小
scaleDownAnimation.duration = 1;
scaleDownAnimation.fromValue = @3;
scaleDownAnimation.toValue = @1.0;
animationGroup.animations = @[scaleUpAnimation,scaleDownAnimation];
// 图片
[self.imageView.layer addAnimation:animationGroup forKey:@"groups"];