filtering single and double taps

前端 未结 4 1618
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 03:59

When the user single taps my view, i need one specific method to run. When the user double taps, i need another method do take place.

The problem is that the double

4条回答
  •  暖寄归人
    2020-12-09 04:38

    Swift 3 Solution:

    doubleTap = UITapGestureRecognizer(target: self, action:#selector(self.doubleTapAction(_:)))
    doubleTap.numberOfTapsRequired = 2
    
    
    singleTap = UITapGestureRecognizer(target: self, action:#selector(self.singleTapAction(_:)))
    singleTap.numberOfTapsRequired = 1
    
    singleTap.require(toFail: doubleTap)
    
    self.view.addGestureRecognizer(doubletap)
    self.view.addGestureRecognizer(singleTap)
    

    In the code line singleTap.require(toFail: doubleTap) we are forcing the single tap to wait and ensure that the tap even is not a double tap. Which means we are asking to ensure the double tap event has failed hence it is concluded as a single tap.

提交回复
热议问题