【问题标题】:UILongPressGestureRecognizer not recognizing .ended stateUILongPressGestureRecognizer 无法识别 .ended 状态
【发布时间】:2019-01-10 23:56:00
【问题描述】:

正如标题所述,我的问题是我的 UILongPressGestureRecognizer 有时无法运行 sender.state = .ended 内的代码。 .began 总是运行和工作。我试图注意到模式但不常见,我还没有找到有效的模式或因果关系。我只需将我的 UITapGestureRecognizer UILongPressGestureRecognizer 添加到我的按钮:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
tapGesture.numberOfTapsRequired = 1
camButton.addGestureRecognizer(tapGesture)

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
longGesture.minimumPressDuration = 0.10
camButton.addGestureRecognizer(longGesture)

然后这是我的 longTap 函数:

    @objc func longTap(_ sender: UIGestureRecognizer) {
            if sender.state == .ended {
                if movieOutput.recordedDuration.seconds == lastRecordDuration || movieOutput.recordedDuration.seconds <= 0.35 {
                    capturePhoto()
                    } else {
                    stopRecording()
                }
           } else if sender.state == .began {
            startCapture()
        }
    }

我将 longPress 用于视频和照片,而 TapGesture 仅用于照片。我正在使用 AVFoundation。

【问题讨论】:

  • canceled 状态呢?如果不添加点击手势怎么办?这对长按有什么改变吗?
  • 不,我有 96.5% 的信心认为它不会改变长手势识别器。我故意将 longGesture 最小保持设置为 0.10,以便它与 normalTapGesture 的重叠更少。但是,如果您认为它正在识别 tapGesture 而不是 longGesture,只需知道 .began 确实 实际上运行它只是从未 .ended 运行,并且在录制一段时间时发生了这种情况。跨度>
  • 您可以尝试一个简单的输出命令,而不是捕获,例如“print("end")"。也许这可以将问题与复杂分开
  • @Guilermoramirezblonski 但是canceled 状态(或任何其他状态)呢?
  • @rmaddy 我目前在我的代码中没有这些活动,但是我没有调查那些发件人状态。我应该实施它们吗?因此,如果它取消,它应该具有与 .ended 相同的代码?

标签: ios swift avfoundation uilongpressgesturerecogni


【解决方案1】:

在得到@rmaddy 的帮助后,解决方案基本上是实现一个 .cancelled 状态动作。由于某种原因,UILongPressGesture 的连续手势被取消了。在我的代码中,我实现了一个 `if sender.state == .cancelled.

【讨论】:

    【解决方案2】:

    我知道这是个老问题,但写给像我这样面临问题的人。

    点击结束时,您可以获得sender.state == .possible

    【讨论】:

      猜你喜欢
      • 2020-09-01
      • 2019-01-19
      • 2020-12-20
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多