在 Tableview 单元格上的长按手势上禁用 didSelectRowAtIndexPath

Mr.*_*ean 5 uitableview ios swift uilongpressgesturerecogni

我一直在尝试在 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
    }
} }
Run Code Online (Sandbox Code Playgroud)

Mr.*_*ean 4

@optimus评论帮助我解决了这个问题,这是一个非常真实的情况,当在tableview单元格上识别长按手势时禁用didSelectRowAtIndexPath,这在互联网上尚未得到解答

override func viewDidLoad() {

    super.viewDidLoad()
    setupLongPress()
    setupTapPress()
}

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 setupTapPress() {

    singlePressGesture = UITapGestureRecognizer(target: self, action: #selector(tapPress))
    singlePressGesture.delegate = self
    singlePressGesture.cancelsTouchesInView = false
    self.tableView.addGestureRecognizer(singlePressGesture)
}

func tapPress(gesture: UIGestureRecognizer) {

    if gesture.state == .ended {
        let touchPoint = gesture.location(in: self.tableView)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {
            // do your task on single tap
        }
    }
}

func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {

    if longPressGestureRecognizer.state == UIGestureRecognizerState.began {

        let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {
            self.showActionSheet()
        }
    }
} }
Run Code Online (Sandbox Code Playgroud)