左右晃动抖动的视图

2025-10-21 22:31:46

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)];

左右晃动抖动的视图

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢