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

后端 未结 14 1048
悲哀的现实
悲哀的现实 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:37

    According to @lostInTransit I prepared code in Swift

    var tapCount:Int = 0
    var tapTimer:NSTimer?
    var tappedRow:Int?
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //checking for double taps here
        if(tapCount == 1 && tapTimer != nil && tappedRow == indexPath.row){
            //double tap - Put your double tap code here
            tapTimer?.invalidate()
            tapTimer = nil
        }
        else if(tapCount == 0){
            //This is the first tap. If there is no tap till tapTimer is fired, it is a single tap
            tapCount = tapCount + 1;
            tappedRow = indexPath.row;
            tapTimer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "tapTimerFired:", userInfo: nil, repeats: false)
        }
        else if(tappedRow != indexPath.row){
            //tap on new row
            tapCount = 0;
            if(tapTimer != nil){
                tapTimer?.invalidate()
                tapTimer = nil
            }
        }
    }
    
    func tapTimerFired(aTimer:NSTimer){
    //timer fired, there was a single tap on indexPath.row = tappedRow
        if(tapTimer != nil){
            tapCount = 0;
            tappedRow = -1;
        }
    }
    

提交回复
热议问题