Swift - 如何识别单击和双击之间的区别

时间:2017-07-12 23:36:15

标签: ios swift taps

我知道有些帖子可以解决单击和双击问题之间的识别差异,但它们都已过时或在c ++中。所以,我想知道如何识别单击和双击之间的区别,因为每次双击系统都认为它是一个点击。我确实将numberOfTaps的值设置为1表示单击,2表示双击。

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(respondToTapGesture(gesture:)))
    view.addGestureRecognizer(tap)

    tap.numberOfTapsRequired = 1

let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(respontToDoubleTapGesture(gesture:)))
    view.addGestureRecognizer(doubleTap)

    doubleTap.numberOfTapsRequired = 2

2 个答案:

答案 0 :(得分:6)

为了识别所采取的操作或区分单击和双击,您需要使手势失败,只需在下面添加该代码

tap.require(toFail: doubleTap) 

答案 1 :(得分:1)

问题是你有两个不同的识别器试图识别手势。

此代码将允许每个代码工作:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

您需要确保已将控制器声明为手势识别代理:

class FromDB: UICollectionViewController, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate {
}

你还需要单击才能点按双击:

tap.require(toFail: doubleTap)