tvos not detecting uiPressTypeSelect

I have implemented 2 different methods and neither can give me a result when the main select button is pressed on siri remote. I am pressing a button and want to detect when 'button down' and 'button up'.


I can detect when both play/pause & menu buttons are pressed but no result for select button. Has me stumped 😕



UITapGestureRecognizer *selectButtonGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    selectButtonGesture.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeSelect], [NSNumber numberWithInteger:UIPressTypePlayPause]];
    [self.view addGestureRecognizer:selectButtonGesture];


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




- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIEvent *)event {
  
    for (UIPress *item in presses)
    {
        NSLog(@"item = %@", item);
    }
  
}

Replies

Do you have any other gesture recognizers (or views that implement pressesBegan:) in the view hierarchy, specifically any other gestures that are also listening for the Select button?

None at all. Just 3 buttons on a page and trying to detect button down/up events on those buttons

The button installs a Select tap gesture recognizer on itself, so that may be the source of the trouble: there are two gesture competing for the same event.


You could either use the normal UIControl API addTarget:action:forControlEvents: with the control event UIControlEventPrimaryActionTriggered to get the Select press, or you could allow your gesture to recognize simultaneously with the buttons. Just be aware that having the gestures recognize simultaneously means the button will fire it's action *and* your gesture will be triggered.

I understand what you are saying.

Just suprised that select button presses would not be triggered on pressesBegan: event when pressing buttons.


For some reason when my mouse cursor hovers over select button and i press the option button it triggers some sort of press. What button is this activating?

How did you detect the play/pause buttons? I am trying to do this and don't know how to implement it.


Thanks for your help.

I tried to do this for the tap gesture...



UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playPause)];

tap.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause]];

[tap addTarget:self action:@selector(playPause) forControlEvents:UIControlEventPrimaryActionTriggered];


But I get a warning when adding the gesture on the last line that says there is no visible interface for UITapGestureRecognizer.


Could you please tell me what I could change it to?


Thank you!

Gesture recognizers don't implement the addTarget:action:forControlEvents: method, so the compiler is telling you that you're trying to call a method that doesn't exist.


The way you attach a gesture recognizer to a view is with addGestureRecognizer:, like this:


[myView addGestureRecognizer:tap];

I recently figured that one out, from the documentation of UIPress, pressEvent enum:


0: Arrow up

1: Arrow down

2: Arrow left:

3: Arrow rigt

4: Select

5: Menu:

6: Play/Pause


You can check for these in pressesBegan:WithEvent: with a switch case on pressEvent 🙂