/// 开始旋转动画,效果:Y轴360度旋转后停留2秒再继续旋转
- (void) startRotationAnimation {
// 添加旋转动画
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
rotationAnimation.fromValue= [NSNumber numberWithFloat:0];
rotationAnimation.toValue= [NSNumber numberWithFloat:M_PI*2];
rotationAnimation.duration=1.5;// 旋转动画1.5秒完成
rotationAnimation.repeatCount=1;
// 创建动画组
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations=@[rotationAnimation];
animationGroup.duration = 3.5; //间隔2秒后再次开始旋转动画
animationGroup.repeatCount=HUGE_VALF;
[self.layer removeAnimationForKey:@"RotationAnimation"];
[self.layer addAnimation:animationGroup forKey:@"RotationAnimation"];
}
/// 缩放动画,效果:1-1.2倍循环缩放
- (void) startScaleAnimation {
// 缩放动画
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue= [NSNumber numberWithFloat:1.0];
scaleAnimation.toValue= [NSNumber numberWithFloat:1.2];
scaleAnimation.duration=0.4;
scaleAnimation.autoreverses=YES;
scaleAnimation.repeatCount=HUGE_VALF;
// 创建动画组
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations=@[scaleAnimation];
animationGroup.duration=0.8;
animationGroup.repeatCount=HUGE_VALF;
[self.layer removeAnimationForKey:@"ScaleAnimation"];
[self.layer addAnimation:animationGroup forKey:@"ScaleAnimation"];
}
/// 晃动效果动画
- (void) startShakeAnimation{
// 晃动动画
CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
shakeAnimation.values=@[@(-0.3),@(0.3),@(-0.3)];// 晃动角度
shakeAnimation.keyTimes=@[@(0),@(0.5),@(1)];// 晃动时机
shakeAnimation.autoreverses=YES;
shakeAnimation.duration=0.5;
// 创建动画组
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations=@[shakeAnimation];
animationGroup.duration=2.0;
animationGroup.repeatCount=HUGE_VALF;
[self.layer removeAnimationForKey:@"ScaleAnimation"];
[self.layer addAnimation:animationGroup forKey:@"ScaleAnimation"];
}
/// 添加白光闪过动画
- (void) startWhiteLightAnimation{
for(CALayer*layer in self.layer.sublayers){
if([layer.name isEqualToString:@"whiteLightLayer"]){
[layer removeFromSuperlayer];
break;
}
}
CALayer*whiteLightLayer = [CALayer layer];
whiteLightLayer.backgroundColor= [[UIColor whiteColor] colorWithAlphaComponent:0.7].CGColor;
whiteLightLayer.frame=CGRectMake(5,0,self.frame.size.width-10,3);
whiteLightLayer.opacity=0.0;
whiteLightLayer.name=@"whiteLightLayer";
[self.layer addSublayer:whiteLightLayer];
// 创建一个 CATransform3D 对象来设置倾斜的角度
CATransform3D transform = CATransform3DIdentity;
transform =CATransform3DRotate(transform, -M_PI_4,0.0,0.0,1.0);
whiteLightLayer.transform= transform;
// 改变位置动画
CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
positionAnimation.fromValue= [NSValue valueWithCGPoint:CGPointMake(0,0)];
positionAnimation.toValue= [NSValue valueWithCGPoint:CGPointMake(self.frame.size.width,self.frame.size.height)];
positionAnimation.duration=0.5;
positionAnimation.autoreverses=YES;
// 改变透明度动画
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.fromValue=@(1.0);
opacityAnimation.toValue=@(0.0);
opacityAnimation.duration=0.5;
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations=@[opacityAnimation, positionAnimation];
animationGroup.duration=1.5;// 持续时间
animationGroup.repeatCount=HUGE_VALF;
[whiteLightLayer addAnimation:animationGroup forKey:@"WhiteLightAnimation"];
}