有时候登录失败,或者未点击同意用户协议需要用到如下的抖动效果,接下来我们来制作。
![](https://img-blog.csdnimg.cn/img_convert/c4f1f904dc028a8eaa53c26b3756b203.gif)
抖动效果,这里封装成一个函数,创建CAKeyframeAnimation,根据x轴进行运动,设置好运动的时间,values写入运动轨迹
- (CAKeyframeAnimation *)shakeAnimation {
CAKeyframeAnimation *shakeAnim = [CAKeyframeAnimation animation];
//根据x轴运动
shakeAnim.keyPath = @"transform.translation.x";
//设置运动时间
shakeAnim.duration = 0.2;
CGFloat delta = 10;
//设置运动轨迹
shakeAnim.values = @[@0, @(-delta), @0, @(delta), @0];
//重复次数
shakeAnim.repeatCount = 2;
return shakeAnim;
}
调用方法如下,创建一个按钮,并写入到点击事件中,添加动画。
_btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
_btn.center = self.view.center;
_btn.backgroundColor = [UIColor redColor];
_btn.layer.cornerRadius = 25;
_btn.layer.masksToBounds = YES;
_btn.tag = -1;
[self.view addSubview:_btn];
[_btn addTarget:self action:@selector(shakeBtn) forControlEvents:UIControlEventTouchUpInside];
- (void)shakeBtn {
[_btn.layer addAnimation:[self shakeAnimation] forKey:nil];
」