如何在拖动时取消突出显示 table 单元格

How to unhighlight table cell while dragging

我们如何取消突出显示 点击+按住+拖动 上的单元格。我正在使用以下代码更改单元格内视图的背景以赋予其突出显示效果。当我点击单元格时,按住并拖动它应该不会突出显示。

我尝试使用 didUnhighlightRowAt,但它在点击单元格时取消突出显示的速度太快。

不是长按手势。请参阅 Apple 的设置应用程序。按住任何菜单,它会突出显示,然后开始拖动,它会取消突出显示该单元格。这就是我想要实现的。

如何实现?

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
    if let cell  = tableView.cellForRow(at: indexPath) as? TableViewCell {
        cell.bgView.backgroundColor = .red
    }
}

滚动开始时触摸突出显示/取消突出显示是 table 视图单元格的默认行为。

通过实施 didHighlightRowAt,您正在抵消这种行为。

相反,在您的单元格 class 中,实施 setHighlighted:

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    UIView.animate(withDuration: 0.3, animations: {
        self.bgView.backgroundColor = highlighted ? .red : .clear
    })
}