How to add detect button presses in tvOS?
Asked Answered
C

6

5

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

Coates answered 10/9, 2015 at 13:15 Comment(2)
Have you read the documentation?Sastruga
Yes @DanielStorm, I now have a pressesEnded function which never gets called for some reasomCoates
S
6

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.

Saran answered 11/9, 2015 at 2:2 Comment(3)
Thank you @jameson! Works perfectly, thanks for the tutorials as well :-)Coates
The templates I've seen don't have it but you can definitely add an event listener for play to detect the Play/Pause button being pressed.Jughead
hello there, if my document has two buttons, OK and cancel, how can I detect clicking one of them? new to JSEpicarp
J
3
let playPauseRecognizer = UITapGestureRecognizer(target: self, action: "playPauseRecognizer:")
playPauseRecognizer.allowedPressTypes = [NSNumber(integer:UIPressType.PlayPause.rawValue)]

     view.addGestureRecognizer(playPauseRecognizer)
Jughead answered 7/1, 2016 at 20:51 Comment(0)
S
2

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

}
Sidney answered 10/9, 2015 at 13:25 Comment(3)
Thanks! I now have this function that isn't getting called, do you know why?Coates
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) { let press = UIPress() abort() }Coates
I am trying to handle the touch of a UIButton on tvOS with that method, but it does not work. Should it not intercept the touch of a button, as I am using UIPressEventSelect to select a button?Vera
F
1

You shouldn't be using "UIControlEvents.TouchUpInside" on tvOS, since that doesn't do what you might expect: you probably want to use "UIControlEvents.PrimaryActionTriggered" instead.

TouchUpInside is triggered by actual touches, which isn't really what you wanted here: if you want the Select button press on the remote to trigger your button, you should use PrimaryActionTriggered.

Fascicule answered 2/11, 2015 at 5:52 Comment(0)
I
0

You can add action to a UIButton same as iOS, according to Apple Docs

An instance of the UIButton class implements a button on the touch screen. A button intercepts touch events and sends an action message to a target object when tapped. Methods for setting the target and action are inherited from UIControl. This class provides methods for setting the title, image, and other appearance properties of a button. By using these accessors, you can specify a different appearance for each button state.

Interfuse answered 10/9, 2015 at 16:57 Comment(0)
S
0

Swift 3 version of @jess-bowers code:

override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
    let anyPress: UIPress? = presses.first
}
Spitz answered 27/4, 2017 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.