layer动画(二)

本文详细介绍了如何在iOS应用中使用Layer动画实现点赞功能,包括取消button的默认高亮效果以及点赞的实现过程。此外,还讲解了如何通过长按手势实现视图的抖动动画,为用户交互增加趣味性。
摘要由CSDN通过智能技术生成

layer动画

三:点赞, button.adjustsImageWhenHighlighted = NO 取消button自带的点击效果

 // 点赞
    self.goodButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.goodButton.frame = CGRectMake(50, 25, 50, 50);
    [self.goodButton addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
    [self.goodButton setBackgroundImage:[UIImage imageNamed:@"IconGood.png"] forState:UIControlStateNormal];
    [self.view addSubview:self.goodButton];
  
    // 把button自带的点击效果取消掉, button类型要设成custom
    self.goodButton.adjustsImageWhenHighlighted = NO;
    self.isSelect = NO;

2.点赞的实现

// 点赞
- (void)changeImage:(UIButton *)button{
    // 关键帧动画
    // 用动画完成放大的效果
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    // 需要给它设置一个关键帧的值,这个值就是变化过程
    // values是一个数组
    animation.values = @[@(0.5), @(1.0), @(1.7)];
    // 设置动画时长
    animation.duration = 0.2;
    // 加到button上
    [self.goodButton.layer addAnimation:animation forKey:@"animation"];
    // 根据状态更换图片
    if (self.isSelect == NO) {
        [self.goodButton setBackgroundImage:[UIImage imageNamed:@"IconHgood.png"] forState:UIControlStateNormal];
    } else {
        [self.goodButton setBackgroundImage:[UIImage imageNamed:@"IconGood.png"] forState:UIControlStateNormal];
    }
    self.isSelect = !self.isSelect;
}

四:抖动

 self.myView = [[UIView alloc] initWithFrame:CGRectMake(100, 430, 150, 50)];
    [self.view addSubview:self.myView];
    self.myView.backgroundColor = [UIColor redColor];
    // 长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.myView addGestureRecognizer:longPress];

2.长按手势里实现抖动

- (void)longPress:(UILongPressGestureRecognizer *)longPress{
    // 关键帧动画
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    float top = M_PI / 20;
    float bom = -M_PI / 20;
    // 抖动的范围
    animation.values = @[@(top), @(0), @(bom), @(0), @(top)];
    // 设置动画的时长
    animation.duration = 0.1;
    // 次数
    animation.repeatCount = NSIntegerMax;
    [self.myView.layer addAnimation:animation forKey:@"key"];
    // 停止动画
    // dispatch_after的作用就是会延迟执行内容
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 在block里写停止动画的方法
        [self.myView.layer removeAnimationForKey:@"key"];
    });
    
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值