Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Apple TV Siri Remote button presses?

According to the Apple TV interface guideline, when it comes to games you're supposed to use the menu button as a pause button when you're not at the main menu of the game (in which case it should return to the Apple TV OS menu). However, I can't find anywhere how you're supposed to detect the hard button input from your remote (as opposed to soft buttons on screen).

I did find this short programming guide to using controllers that almost seems to imply that you're supposed to use the remote as a controller in this case, but I can't help but think there's a simpler way. ex.

 -(void)buttonPressBegan:(NSEvent*)event

etc (that's not real... I'm just hoping there's something like that). What is/Is there a sanctioned way of detecting this?

like image 749
mredig Avatar asked Sep 15 '15 02:09

mredig


3 Answers

Apple suggests using a UITapGestureRecognizer to detect when a button is released.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController {
    UITapGestureRecognizer *tapRecognizer;
}

-(void)viewDidLoad {
    [super viewDidLoad];

    tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    tapRecognizer.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeMenu]];
    [self.view addGestureRecognizer:tapRecognizer];
}

-(void)handleTap:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Menu button released");
    }
}

For a complete list of UIPressType's refer to UIPress Class Reference.

like image 152
Daniel Storm Avatar answered Oct 26 '22 05:10

Daniel Storm


You're close! These are the methods you want: they work basically just like the touch equivalents.

- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIEvent *)event;
- (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(UIEvent *)event;
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIEvent *)event;
- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIEvent *)event;
like image 26
Justin Voss Avatar answered Oct 26 '22 07:10

Justin Voss


If you are using something like a UISplitViewController the event detection will happen on the "DetailViewController". But the view controller will still be dismissed! This is to detect that the MENU button was pressed and not override its behaviour.

 override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    guard let type = presses.first?.type else {
        return
    }

    switch type {
    case UIPressType.Menu :
        //Handle this here
    default : break

    }
}
like image 37
apinho Avatar answered Oct 26 '22 07:10

apinho