【问题标题】:Is there a way to have a two tap gesture recognizers with different number of taps [duplicate]有没有办法让两个点击手势识别器具有不同的点击次数[重复]
【发布时间】:2018-01-02 07:55:54
【问题描述】:

在我的应用程序中,我需要根据用户点击图像的次数在点击手势上运行不同的功能。

我有一个需要点击 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)

【问题讨论】:

  • 确保这一点 -> postVideoView .isUserIntraction = true
  • 这不是问题。我更新了描述以进一步澄清问题
  • stackoverflow.com/questions/8876202/… 您应该只将require(toFail:) 放在带有双击参数的单击上。

标签: ios iphone swift uitapgesturerecognizer


【解决方案1】:

您需要确保双击手势失败才能初始化 tapToWatch

试试这个

tapToWatch.require(toFail: doubleLike)

【讨论】:

  • 啊,有道理,谢谢!
【解决方案2】:

我现在没有尝试,但据我所知,您只需要在单个识别器上设置 requiretofail,如 this answer

【讨论】:

  • 您最好将其添加为评论,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多