有没有办法让两次点击手势识别器具有不同的点击次数
Is there a way to have a two tap gesture recognizers with different number of taps
在我的应用中,我需要 运行 根据用户点击图像的次数对点击手势执行不同的功能。
我有一个需要点击 1 次的点击手势识别器。
我的第二个点击手势识别器需要点击 2 次。但是,如果我点击它 2 次,则会调用每个点击手势的两个函数。
有什么方法可以做到,两个函数可以分开调用,互不干扰?
问题:如果我点击图像两次,它会同时调用 goWatchVideo 函数和 onDoubleTap 函数。我想避免这种情况。
// Gesture that requires 1 tap
let tapToWatch = UITapGestureRecognizer(target: self, action: #selector(goWatchVideo))
tapToWatch.numberOfTapsRequired = 1
postVideoView.addGestureRecognizer(tapToWatch)
// Gesture that requires 2 taps
let doubleLike = UITapGestureRecognizer(target: self, action:#selector(self.onDoubleTap))
doubleLike.numberOfTapsRequired = 2
postVideoView.addGestureRecognizer(doubleLike)
// Tried to set them up for failures
tapToWatch.require(toFail: doubleLike)
doubleLike.require(toFail: tapToWatch)
您需要确保双击手势失败才能初始化 tapToWatch
试试这个
tapToWatch.require(toFail: doubleLike)
我现在没有尝试,但据我所知,你只需要在单个识别器上设置 requiretofail,如 this answer
在我的应用中,我需要 运行 根据用户点击图像的次数对点击手势执行不同的功能。
我有一个需要点击 1 次的点击手势识别器。
我的第二个点击手势识别器需要点击 2 次。但是,如果我点击它 2 次,则会调用每个点击手势的两个函数。
有什么方法可以做到,两个函数可以分开调用,互不干扰?
问题:如果我点击图像两次,它会同时调用 goWatchVideo 函数和 onDoubleTap 函数。我想避免这种情况。
// Gesture that requires 1 tap
let tapToWatch = UITapGestureRecognizer(target: self, action: #selector(goWatchVideo))
tapToWatch.numberOfTapsRequired = 1
postVideoView.addGestureRecognizer(tapToWatch)
// Gesture that requires 2 taps
let doubleLike = UITapGestureRecognizer(target: self, action:#selector(self.onDoubleTap))
doubleLike.numberOfTapsRequired = 2
postVideoView.addGestureRecognizer(doubleLike)
// Tried to set them up for failures
tapToWatch.require(toFail: doubleLike)
doubleLike.require(toFail: tapToWatch)
您需要确保双击手势失败才能初始化 tapToWatch
试试这个
tapToWatch.require(toFail: doubleLike)
我现在没有尝试,但据我所知,你只需要在单个识别器上设置 requiretofail,如 this answer