在Tableview Cell上长按手势时禁用didSelectRowAtIndexPath

时间:2018-02-14 12:24:52

标签: ios swift uitableview 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
    }
} }

2 个答案:

答案 0 :(得分:1)

@optimus评论帮助我解决了这个问题,这是一个非常真实的情况,当在桌面视频上识别出没有在互联网上回答的长按手势时禁用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()
        }
    }
} }

答案 1 :(得分:0)

试试这个!

override func dismiss(animated flag: Bool,
                          completion: (() -> Void)?)
    {
        super.dismiss(animated: flag, completion:completion)

        self.tableView.allowsSelection = true
    }

添加此覆盖方法

==
相关问题