playPauseTapGestureRegocnizer

playPauseTapGestureRegocnizer

PlayPause按钮没有响应。
这是我放在viewDidLoad方法中的代码:

playPauseTapGestureRegocnizer = UITapGestureRecognizer(target: self, action: #selector(playPauseTapped))
playPauseTapGestureRegocnizer.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue)]
view.addGestureRecognizer(playPauseTapGestureRegocnizer)


我尝试将其放置在多种方法中,但不起作用。
奇怪的是,当我用playPause更改menu时,它可以与菜单按钮完美配合。

我想念什么吗?

最佳答案

斯威夫特4

PlayPause Gesture无法在实际设备上运行。
另一种解决方案是听UIPressesEvent

override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?)
{
    for press in presses
    {
        if press.type == UIPress.PressType.playPause
        {
            // handle play pause action
            break
        }
        else
        {
            super.pressesEnded(presses, with: event)
        }
    }
}

10-08 11:22