【问题标题】:Disable didSelectRowAtIndexPath on Long press gesture on Tableview Cell在 Tableview Cell 上的长按手势上禁用 didSelectRowAtIndexPath
【发布时间】:2018-02-14 12:24:52
【问题描述】:

我一直在尝试在 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
    }
} }

【问题讨论】:

  • 在tableview单元格上添加点击手势,然后它将禁用tableview的didSelect
  • 为单击添加轻按手势,并在轻按时执行您想要的操作..

标签: ios swift uitableview uilongpressgesturerecogni


【解决方案1】:

@optimus 评论帮我解决了这个问题,这是一个非常真实的情况,当在 Internet 上没有回答的 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()
        }
    }
} }

【讨论】:

    【解决方案2】:

    试试这个!

    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
        }
    }
    

    添加这个覆盖方法

    override func dismiss(animated flag: Bool,
                              completion: (() -> Void)?)
        {
            super.dismiss(animated: flag, completion:completion)
    
            self.tableView.allowsSelection = true
        }
    

    【讨论】:

    • 嘿@Mr.Bean 你检查了dismiss是否被断点调用。并确保您的代码以手势结束状态注释
    猜你喜欢
    • 2018-01-18
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    相关资源
    最近更新 更多