【问题标题】:How to access Siri Remote play/pause button and override menu button如何访问 Siri Remote 播放/暂停按钮和覆盖菜单按钮
【发布时间】:2016-04-23 15:21:57
【问题描述】:

如何使用 Siri 遥控器访问播放/暂停按钮并覆盖菜单按钮?我目前正在使用它,但它对我不起作用。当我使用此代码时,我的程序崩溃了,但只有当我将其称为四个示例时才按下暂停按钮 编码器当前位于 touchesBegan 旁边的 didMoveToView 下方

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

【问题讨论】:

标签: swift tvos uitapgesturerecognizer siri-remote


【解决方案1】:

您的问题是您正在调用一个名为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")
    }
}

【讨论】:

  • 好的,我试过了,它仍然会导致崩溃。让 tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:") tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)]; self.view!.addGestureRecognizer(tapGesture) func handleTap(sender: UITapGestureRecognizer) { print("button tapped") }
  • 似乎选择器没有正确调用函数 GameScene handleTap:]: unrecognized selector sent to instance 0x126733890
  • 奇怪的是仍然无法正常工作;我使用了不同的方式来做我想做的事,使用触摸板,但我仍然收到相同的错误消息。
  • @FerdinandLösch 没问题。您应该为未来的读者发布您的解决方案的答案。
【解决方案2】:

我将以下内容用于 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")
    }
}

【讨论】:

    【解决方案3】:

    我通过将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")
        }     
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2011-11-12
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多