Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tvos Overriding pressesBegan

Tags:

button

menu

tvos

I think I'm missing something pretty basic, but I can't get button presses to override properly.

I am trying to detect the menu button press, so I override pressesBegan: and test for the UIPressTypeMenu. The method catches the menu button press, and performs my code, but then immediately follows with an app exit anyway.

Here is my overridden method. Does anybody know what i'm doing wrong? Is there something else I need to override the automatic menu exit? It's almost like there are two responders. Same deal with the left tap and right tap. They are recognized, my code fires, but then the focus changes anyway because I haven't completely handled the event.

I tried the code in the app's first view controller and the menu is overridden properly. Then, I set up a blank view and present that view as view number 2 and I cannot get the methods to override.

- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {

    for(UIPress *press in presses) {
        if(press.type == UIPressTypeMenu) {

            [(UIImageView *) [self.view viewWithTag:100] setUserInteractionEnabled:NO];

        }
        if(press.type == UIPressTypePlayPause) {
            // play and pause code
        }
    } 
}


-(void) pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {

    // 
    for(UIPress *press in presses) {
        if(press.type == UIPressTypeMenu) {
            // do ButtonA job
            NSLog(@"test");
        }
        if(press.type == UIPressTypePlayPause) {
            // do ButtonX job
        } else {


        }
    }

}

-(void) pressesChanged:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {

    // Works fine!
    for(UIPress *press in presses) {
        if(press.type == UIPressTypeMenu) {
            // do ButtonA job
        }
        if(press.type == UIPressTypePlayPause) {
            // do ButtonX job
        }
    }

}

-(void) pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {

    // Works fine!
    for(UIPress *press in presses) {
        if(press.type == UIPressTypeMenu) {
            // do ButtonA job
            NSLog(@"cancelled");
        }
        if(press.type == UIPressTypePlayPause) {
            // do ButtonX job
        }
    }
}
like image 635
jpsquare Avatar asked Oct 31 '22 17:10

jpsquare


1 Answers

The app is exiting because you're allowing the pressesEnded:withEvent: message to get back up to the top of the responder chain. You should also override that method, and do not call super if you don't want the app to exit (be careful here, though: if the user can't use Menu to exit, they will be extremely frustrated with you)

The left and right arrows are triggering focus changes because those are handled with gesture recognizers, not with pressesBegan / pressesEnded methods, so it doesn't matter if your view overrides those.

The Event Handling Guide for iOS has more details about how gesture recognizers and the UIResponder methods interact with each other.

like image 73
Justin Voss Avatar answered Nov 08 '22 04:11

Justin Voss