iOS 点击按钮的抖动动画

#pragma mark - **************** 列表中按钮点击动画效果

-(void)shakeToShow:(UIButton *)button{

    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];

    animation.duration = 0.5;

    

    NSMutableArray *values = [NSMutableArray array];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];

    animation.values = values;

    [button.layer addAnimation:animation forKey:nil];

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 桌面上,当你长按一个应用程序图标时,应用程序会开始抖动并显示一个“X”按钮,以便你可以删除应用程序或将其移动到新位置。下面是如何为 iOS 桌面编辑添加抖动动画的步骤: 1. 创建编辑模式 首先,你需要创建一个编辑模式,该模式将允许用户在屏幕上移动应用程序图标和删除应用程序。可以使用以下代码创建编辑模式: ```swift func setEditing(_ editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) collectionView?.allowsMultipleSelection = editing let indexPaths = collectionView?.indexPathsForVisibleItems ?? [] for indexPath in indexPaths { let cell = collectionView?.cellForItem(at: indexPath) as? AppIconCell cell?.isEditing = editing } } ``` 2. 添加抖动动画 要为编辑模式添加抖动动画,可以使用以下代码: ```swift private func animateShake(for cells: [UICollectionViewCell]) { for cell in cells { let shakeAnimation = CABasicAnimation(keyPath: "position") shakeAnimation.duration = 0.08 shakeAnimation.repeatCount = 2 shakeAnimation.autoreverses = true shakeAnimation.fromValue = NSValue(cgPoint: CGPoint(x: cell.center.x - 5, y: cell.center.y)) shakeAnimation.toValue = NSValue(cgPoint: CGPoint(x: cell.center.x + 5, y: cell.center.y)) cell.layer.add(shakeAnimation, forKey: "position") } } ``` 该函数将遍历所有应用程序图标单元格,并为每个单元格添加一个基本的抖动动画。你可以自定义抖动动画的属性,例如持续时间、重复计数和偏移量。 3. 触发抖动动画 最后,在编辑模式下,你需要在用户长按应用程序图标时触发抖动动画。可以使用以下代码实现: ```swift override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) { super.collectionView(collectionView, didHighlightItemAt: indexPath) if isEditing { let cells = collectionView.visibleCells animateShake(for: cells) } } ``` 该函数将检查编辑模式是否处于活动状态。如果是,则遍历所有应用程序图标单元格并为每个单元格添加抖动动画。 希望这可以帮助你添加 iOS 桌面编辑的抖动动画

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值