How can I detect a double tap on a certain cell in UITableView?

后端 未结 14 1054
悲哀的现实
悲哀的现实 2020-12-01 00:18

How can I detect a double tap on a certain cell in UITableView?

i.e. I want to perform one action if the user made a single touch and a

14条回答
  •  醉酒成梦
    2020-12-01 00:48

    Swift 3 solution from compare answers. No need any extensions, just add this code.

    override func viewDidLoad() {
        viewDidLoad()
    
        let doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(sender:)))
        doubleTapGestureRecognizer.numberOfTapsRequired = 2
        tableView.addGestureRecognizer(doubleTapGestureRecognizer)
    
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(sender:)))
        tapGestureRecognizer.numberOfTapsRequired = 1
        tapGestureRecognizer.require(toFail: doubleTapGestureRecognizer)
        tableView.addGestureRecognizer(tapGestureRecognizer)
    }
    
    func handleTapGesture(sender: UITapGestureRecognizer) {
        let touchPoint = sender.location(in: tableView)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {
            print(indexPath)
        }
    }
    
    func handleDoubleTap(sender: UITapGestureRecognizer) {
        let touchPoint = sender.location(in: tableView)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {
            print(indexPath)
        }
    }
    

提交回复
热议问题