iOS删除抖动动画效果
iOS 里的“删除抖动”动画,挺像桌面图标那种轻轻晃动的效果。你见过,长按 App 图标想删掉时,它开始左右摇晃,挺有“别碰我”的感觉。这种交互其实蛮实用,用来提示用户“这里出问题了”或者“可以删除我了”。
UIView 的动画方法是关键,你用UIView.animate
就能轻松实现。思路也简单:在两个位置间来回移动,时间设置短点,再配上.repeat
和.autoreverse
,就抖起来了。
UIView.animate(withDuration: 0.1, delay: 0, options: [.repeat, .autoreverse], animations: {
yourView.center = CGPoint(x: yourView.center.x - 5, y: yourView.center.y)
}, completion: nil)
如果你想让它在长按时才开始抖,那就得搭配UILongPressGestureRecognizer
。监听.began
状态,动画就启动。用起来灵活,响应也快。
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
yourView.addGestureRecognizer(longPressGesture)
@objc func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == .began {
shakeAnimation(yourView)
}
}
另外,如果你图省事,某些系统弹窗动画里自带类似“抖一下”的反馈,用UIPresentationController
就能用到。
,不管是“删除抖动”还是“长按抖动”,目的都一样:给用户一个更直观的操作反馈。动画不用太复杂,重在自然。你调动画时间的时候可以多试几组值,找到最舒服的节奏就行。
如果你对图标的动效有兴趣,还可以看看图标抖动技巧;要是想试试 Swift 的小工具,也推荐去看看Swift 防抖动微型库,用起来也挺顺。
165.62KB
文件大小:
评论区