What events are generated by UIControl on tvOS?

With UIButton tvOS creates UIControlEvents.PrimaryActionTriggered (not .TouchUpInside , as on iOS) when the button receives focus and the user presses on the remote control.

UIControl does not seem to create any of these events. I do not see that he is creating any events, in fact, when the user focuses on the control and the taps on the remote control.

How to use custom UIControl for tvOS?

+5
source share
2 answers

A UIControl does not generate any control events at its discretion. Your subclass is responsible for emitting events (usually by sending itself sendActionsForControlEvents: .

Since UIControl is not currently documented for tvOS, Apple may not want you to subclass it.

Anyway, I played a little with him a bit, but apparently, to implement your own UIControl subclass UIControl you should override canBecomeFocused to return YES , and you should override pressesEnded:withEvent: to act on (presumably by sending yourself sendActionsForControlEvents: )

You can also override pressesBegan:withEvent: to highlight your control.

Since UIControl conforms to the UIFocusEnvironment protocol (via UIView ), you can override didUpdateFocusInContext:withAnimationCoordinator: to update the appearance of the control depending on whether it has focus.

+4
source

I spent the last couple of weeks adding tvOS support for the widget that I packaged with my SVG SVGgh rendering library, so I have some sense for that. The problem is that I think this is a new or new kind of action: UIControlEventPrimaryActionTriggered, which is dispatched when the control wants to report commitment to the action, as in this response to the completion of the UIPress in my GHButton . Remember to connect the selector to this new action in your storyboard.

 -(void) pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event { [self sendActionsForControlEvents:UIControlEventPrimaryActionTriggered]; self.beingPressed = NO; self.selected = self.selected; } 

But in the segmented control analogue, the action is not set when the button is pressed, but when the focused segment changes, as in the following (heavily edited for clarity) code from my GHSegmentedControl class:

 - (BOOL)shouldUpdateFocusInContext:(UIFocusUpdateContext *)context { BOOL result = NO; if(context.nextFocusedView == self) { result = YES; [self highlight:YES]; if(context.focusHeading == UIFocusHeadingRight) { [self incrementValue]; [self.parentContent.control setNeedsFocusUpdate]; [self sendActionsForControlEvents:UIControlEventValueChanged]; } else if(context.focusHeading == UIFocusHeadingLeft) { [self decrementValue]; [self.parentContent.control setNeedsFocusUpdate]; [self sendActionsForControlEvents:UIControlEventValueChanged]; } } return result; } 

Now that I am thinking about this, perhaps I should also send the UIControlEventPrimaryActionTriggered action to the segmented control, but UIControlEventValueChanged seems to work as I expect.

0
source

All Articles