How to detect Apple TV Siri Remote button presses?

有些话、适合烂在心里 提交于 2019-11-29 19:57:53

问题


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?


回答1:


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.




回答2:


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;



回答3:


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

    }
}



回答4:


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

    }
}


来源:https://stackoverflow.com/questions/32576834/how-to-detect-apple-tv-siri-remote-button-presses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!