我一直在尝试在 UITableView 上设置长按手势识别器。手势工作正常,但我想在识别长按手势时禁用 UITableView 的 didSelectRowAtIndexPath 委托功能。
简而言之,如果用户单击单元格,我必须推送一个新的 UIViewController,如果用户长按单元格,我必须显示 UIActionSheet。
extension GroupChatListingViewController : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func setupLongPress() {
longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
self.tableView.addGestureRecognizer(longPressGesture)
}
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
self.tableView.allowsSelection = false
let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
self.showActionSheet()
}
}
else if longPressGestureRecognizer.state == .ended {
self.tableView.allowsSelection = true
}
} }