迅捷解决方案
嗯,大约 1.5 年后我终于想出了一个解决方案。
第 1 步 - 单元 UI 设置
I set up my custom table view cell like this:
- A 和 B 代表滑动操作颜色。
- C 是我将进行左右动画处理的 UIView。
第 2 步 - 将动画添加到单元格
func animateSwipeHint() {
slideInFromRight()
}
private func slideInFromRight() {
UIView.animate(withDuration: 0.5, delay: 0.3, options: [.curveEaseOut], animations: {
self.cellBackgroundView.transform = CGAffineTransform(translationX: -self.swipeHintDistance, y: 0)
self.cellBackgroundView.layer.cornerRadius = 10
}) { (success) in
UIView.animate(withDuration: 0.2, delay: 0, options: [.curveLinear], animations: {
self.cellBackgroundView.transform = .identity
}, completion: { (success) in
// Slide from left if you have leading swipe actions
self.slideInFromLeft()
})
}
}
private func slideInFromLeft() {
UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseOut], animations: {
self.cellBackgroundView.transform = CGAffineTransform(translationX: self.swipeHintDistance, y: 0)
}) { (success) in
UIView.animate(withDuration: 0.2, delay: 0, options: [.curveLinear], animations: {
self.cellBackgroundView.transform = .identity
})
}
}
第 3 步 - 触发动画
In the viewDidLoad
对于具有表视图的视图控制器,我有以下代码:
if self.tableView.visibleCells.count > 0 {
let cell = self.tableView.visibleCells[0] as! TableViewCell
cell.animateSwipeHint()
}
Example:
视频解决方案
如果您想更深入地了解此解决方案,我创建了一个视频:https://youtu.be/oAGoFd_GrxE https://youtu.be/oAGoFd_GrxE