左右晃动抖动的视图
1、首先先要声明一个红色的视图,主要是对这个视图进行操作。
@property (nonatomic , strong) UIView *redView;
2、首先使用基本动画,抖动是因为视图绕着Z轴转动。
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = @(-M_PI_4 / 5);
animation.toValue = @(M_PI_4 / 5);
animation.duration = 2.f;
animation.autoreverses = YES;
animation.repeatCount = CGFLOAT_MAX;
3、将动画添加到这个redView的layer上,视图的动画是以layer的表示出来的。
[self.redView.layer addAnimation:[self shouFirstAnimation_1] forKey:@"one"];
4、接下来使用关键帧动画写上面的效果。
第一种
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.values = @[@(-M_PI_4 / 5),
@(M_PI_4 / 5) ];
animation.duration = 2.f;
animation.autoreverses = YES;
animation.repeatCount = CGFLOAT_MAX;
5、这个使用了回路,才会出现抖动的效果,下面就是删掉回路,然后在values里面加一个初始的值,就可以实现抖动。
animation.values = @[@(-M_PI_4 / 5),
@(M_PI_4 / 5),
@(-M_PI_4 / 5)];