Ferdinand Lösch
Ferdinand Lösch

Reputation: 195

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

How can I access the play/pause button with the Siri remote and override the menu button? I am currently using this, but it is not working for me. My program crashes when I use this code but only when I call it four example pressing the pause button The coders is currently positioned below didMoveToView next to touchesBegan

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

Upvotes: 2

Views: 635

Answers (3)

norq
norq

Reputation: 1434

I use the following for Swift 4 (as per question: @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")
    }
}

Upvotes: 1

Ferdinand Lösch
Ferdinand Lösch

Reputation: 195

I solved my problem by moving the tapRecognizer selector into my previously set up touch handler function so the code looks like this now:

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")
    }     
 }

Upvotes: 0

Daniel Storm
Daniel Storm

Reputation: 18898

Your issue is you're calling a function called handleTap: that receives a parameter but you don't have a function called handleTap:. That's what action represents in this line:

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

Change your func tapped() to:

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

Upvotes: 1

Related Questions