Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add detect button presses in tvOS?

I have follow this tutorial and everything works fine. The only issue I'm having is i can't figure out how to detect when a button has been pressed?

Thanks in advance

like image 776
Alex Chesters Avatar asked Sep 10 '15 13:09

Alex Chesters


3 Answers

Author of the tutorial here, the method of adding interactivity to TVML-based apps is to use addEventListener on the DOM element in question. You can find the DOM element by holding a reference to it during creation, or by using getElementById or other such similar JavaScript DOM techniques. Also, I should mention I've added a "Part 2" to the mentioned tutorial which includes this as it's primary focus.

Here's an example of how you might do this in JS, assuming myDOMElement is a variable that references your button as a DOM element.

  myDOMElement.addEventListener("select", function() { alert("CLICK!") }, false);

I have more info on the tutorial of course, so feel free to check that out, too.

like image 131
Jameson Avatar answered Sep 20 '22 20:09

Jameson


Button presses are similar to UITouch events. Take a look at the UIPressEvent callbacks in the UIResponder header. UIViewControllers are part of the responder chain, so you can add the callbacks in your view controller similar to this:

- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event {

    UIPress *anyPress = [presses anyObject];
    // handle press event

}
like image 29
Jess Bowers Avatar answered Sep 19 '22 20:09

Jess Bowers


let playPauseRecognizer = UITapGestureRecognizer(target: self, action: "playPauseRecognizer:")
playPauseRecognizer.allowedPressTypes = [NSNumber(integer:UIPressType.PlayPause.rawValue)]

     view.addGestureRecognizer(playPauseRecognizer)
like image 27
Nick Avatar answered Sep 21 '22 20:09

Nick