如何访问 Siri Remote play/pause 按钮和覆盖菜单按钮

How to access Siri Remote play/pause button and override menu button

如何使用 Siri 遥控器访问 play/pause 按钮并覆盖菜单按钮?我目前正在使用它,但它对我不起作用。当我使用这段代码时,我的程序崩溃了,但只有当我调用它时才会崩溃 四个示例按下暂停按钮 编码器目前位于 didMoveToView 下面,紧挨着 touchesBegan

let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)

您的问题是您正在调用一个名为 handleTap: 的函数来接收一个参数,但您没有一个名为 handleTap: 的函数。这就是 action 在这一行中所代表的:

let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))

将您的 func tapped() 更改为:

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Ended {
        print("Menu button released")
    }
}

我通过将 tapRecognizer 选择器移动到我之前设置的触摸处理函数中解决了我的问题,因此代码现在如下所示:

private func handleTouches(touches: Set<UITouch>) {
    for touch in touches {
        let touchLocation = touch.locationInNode(self)
        lastTouch = touchLocation

        let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
        tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)];
        self.view!.addGestureRecognizer(tapRecognizer)
    }
}

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Ended {
        print("Menu button released")
    }     
 }

我对 Swift 4 使用以下内容(根据问题:@selector() in Swift?

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)

@objc func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Ended {
        print("Menu button released")
    }
}