如何来回移动UITableViewCell以显示它可以滑动? [英] How to move UITableViewCell back and forth to show it can be swiped?
问题描述
我在某些应用中看到,当您进入带有表格视图的屏幕时,该单元格的简短动画开始被滑动,显示红色的滑动删除"按钮(UIContextualAction按钮),然后恢复正常.它给用户提示:可以扫动这些行."
I see in some apps when you come to a screen with a tableview there's a short animation of the cell starting to be swiped, showing the red "swipe to delete" button (UIContextualAction button) and then it returns to normal. It is giving the user the hint: "These rows can be swiped."
有没有一种方法可以达到这种效果?也许是一种以编程方式开始划行然后取消的方法?
Is there a way to achieve this effect? Maybe a way to programmatically start a row swipe then cancel it?
推荐答案
快速解决方案
嗯,大约1.5年后,我终于想出了一个解决方案.
Swift Solution
Well, about 1.5 years later I finally came up with a solution.
我这样设置自定义表格视图单元:
I set up my custom table view cell like this:
- A和B代表滑动动作的颜色.
- C是我要左右动画的UIView.
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步-触发动画
在具有表视图的视图控制器的 viewDidLoad
中,我有以下代码:
if self.tableView.visibleCells.count > 0 {
let cell = self.tableView.visibleCells[0] as! TableViewCell
cell.animateSwipeHint()
}
示例:
如果您想更深入地了解此解决方案,请创建一个视频: https://youtu.be/oAGoFd_GrxE
I created a video if you'd like a more in-depth walkthrough of this solution: https://youtu.be/oAGoFd_GrxE
这篇关于如何来回移动UITableViewCell以显示它可以滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!