我在某些应用程序中看到,当您进入带有表格视图的屏幕时,该单元格的简短动画开始被滑动,显示红色的“滑动以删除”按钮(UIContextualAction按钮),然后恢复正常。它给用户提示:“这些行可以刷卡。”
有没有办法达到这种效果?也许是一种以编程方式开始划行然后取消的方法?
好吧,大约1.5年后,我终于想出了一个解决方案。
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
})
}
}
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad具有表视图的视图控制器的中,我有以下代码:
if self.tableView.visibleCells.count > 0 {
let cell = self.tableView.visibleCells[0] as! TableViewCell
cell.animateSwipeHint()
}
Run Code Online (Sandbox Code Playgroud)
如果您想进一步了解此解决方案,请创建一个视频:https : //youtu.be/oAGoFd_GrxE
| 归档时间: |
|
| 查看次数: |
1442 次 |
| 最近记录: |