6

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?

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
mredig
  • 1,736
  • 1
  • 16
  • 29

4 Answers4

10

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.

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
  • 1
    Justin Voss also had a correct answer, but this one is more correct. Justin's solution required going through the ViewController and directing the methods to the SpriteKit scene whereas the gesture related one allowed for direct code in the SpriteKit scene. It also has the additional advantage of being able to detect *rough* positional data by returning if a press was in the up/down/left/right direction. The downside is that I can't figure out how to determine began/ended/changed/cancelled like I can with Justin's answer, but that's not immediately important to my project! – mredig Sep 15 '15 at 18:26
  • 1
    Man am I silly... It looks like my primary complaint from the previous comment was actually addressed in the answer. I admit I haven't tested it, but if you can detect the state of the recognizer, that would allow me to detect if it ended or not! – mredig Sep 19 '15 at 03:16
  • 1
    Is there any way to pass the event along to get default behavior after custom logic has executed? For example, I'd like to intercept a menu press, act on it, and then have it execute it's normal behavior. – Rob Reuss Nov 01 '15 at 21:35
  • 1
    Thanks! This is exactly what to use for the Menu Button – Danoli3 Dec 17 '15 at 01:58
3

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;
Justin Voss
  • 6,294
  • 6
  • 35
  • 39
1

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

    }
}
apinho
  • 2,235
  • 3
  • 25
  • 39
1
In Swift 3
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {

    guard let type = presses.first?.type else {
        return
    }

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

    }
}
Naren
  • 1,504
  • 16
  • 19