ios - 在 Tableview Cell 上的长按手势上禁用 didSelectRowAtIndexPath

标签 ios swift uitableview uilongpressgesturerecogni

我一直在尝试在 UITableView 上设置长按手势识别器。手势工作正常,但我想在识别长按手势时禁用 UITableView 的 didSelectRowAtIndexPath 委托(delegate)功能。

简而言之,如果用户单击单元格,我必须推送一个新的 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
    }
} }

最佳答案

@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()
        }
    }
} }

关于ios - 在 Tableview Cell 上的长按手势上禁用 didSelectRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48787187/

相关文章:

ios - 无法在简单的 Times Table 应用程序中显示 TableView 数据

ios - 在 ARKit 框架中设置光照

ios - 如果单击 Swift 中的按钮,我该如何更改图像?

ios - XCode 提示自动补全很慢

IOS Swift Amazon S3 传输实用程序 - nil 与预期的参数类型 nsurl 不兼容

ios - Storyboard中的新 View Controller 中未调用 TableView 委托(delegate)方法

ios - 如果UITableViewCell有多个按钮,那么在哪里处理触摸事件是最好的

iphone - 如何在 UITableViewCell 中创建具有圆形背景的数字(如电子邮件应用程序)?

iOS 7 Core Image QR Code 生成太模糊

c# - 我想从 SQLite DB 获取所有信息