UILongPressGestureRecognizer无法识别.end状态

时间:2019-01-10 23:56:01

标签: ios swift avfoundation uilongpressgesturerecogni

正如标题所示,我的问题是我的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。

1 个答案:

答案 0 :(得分:0)

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