【发布时间】:2016-12-12 03:30:30
【问题描述】:
我的单元格可以使用UIPanGestureRecognizer 进行平移。我的问题是,我只想一次平移一个单元格。为此,我添加了一些尝试:
选项 1,为每个单元格使用 Bool 属性:
class PannableCell: UITableViewCell {
weak var controller: TableViewController?
var isPanning = false
let panGesture = UIPanGestureRecognizer()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
panGesture.addTarget(self, action: #selector(handlePan))
contentView.addGestureRecognizer(panGesture)
}
func handlePan(recognizer: UIPanGestureRecognizer) {
guard let cells = controller?.tableView.visibleCells.filter({ $0 != self }) else { return }
guard !cells.contains(where: { ($0 as? PannableCell)?.isPanning ?? false }) else { return }
if recognizer.state == .began { isPanning = true }
if recognizer.state == .changing { } //moving cell
if recognizer.state == .ended {
UIView.animate(withDuration: 0.5, animations: { }) {
if $0 { self.isPanning = false }
}
}
}
}
使用此代码,一次只能平移一个单元格。不幸的是,在某些情况下(大约 5 - 10%)可以平移多个单元格。我怀疑它与在平移或点击时重复使用单元格有关。
我知道我应该让视图控制器处理 MVC 模型中所述的控制事物。我不确定它是否能解决前面提到的问题。
有什么建议吗?
【问题讨论】:
标签: swift uitableview uipangesturerecognizer