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];
复制代码

效果:

代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值