iOS-类似于App抖动效果

做法有很多 这里利用关键帧动画实现

懒加载方式创建一个UIView,设置其内容为一张图片,给其添加长按手势:

- (UIView *)ditheringView
{
    if (!_ditheringView) {
        _ditheringView = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_Width - 80) * 0.5, (SCREEN_Height - 80) * 0.5, 80, 80)];
        _ditheringView.backgroundColor = [UIColor cyanColor];
        _ditheringView.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"luhan"].CGImage);
        _ditheringView.layer.cornerRadius = 15;
        _ditheringView.layer.masksToBounds = YES;
        
        //添加长按手势
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction)];
        [_ditheringView addGestureRecognizer:longPressGesture];
    }
    return _ditheringView;
}


[self.view addSubview:self.ditheringView];

长按手势:

//长按手势响应事件
- (void)longPressGestureAction
{
    //创建一个关键帧动画
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    //设置关键帧
    keyAnimation.values = @[@(-M_PI_4 * 0.1 * 1), @(M_PI_4 * 0.1 * 1), @(-M_PI_4 * 0.1 * 1)];
    
    //设置重复
    keyAnimation.repeatCount = CGFLOAT_MAX;
    
    //把核心动画添加到layer上
    [self.ditheringView.layer addAnimation:keyAnimation forKey:@"keyAnimation"];
}

移除动画效果代码:

NSLog(@"移除动画");
    
    //移除某一动画效果
    [self.ditheringView.layer removeAnimationForKey:@"keyAnimation"];
    
    //移除所有动画效果
//    [self.ditheringView.layer removeAllAnimations];

效果:

抖动.gif

代码

你可能感兴趣的:(iOS-类似于App抖动效果)