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?
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.
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;
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With