How can I receive touches using tvOS?

user2545883 picture user2545883 · Sep 11, 2015 · Viewed 7.2k times · Source

How can I receive touches using tvOS with the simulator? We need know touch position. UIPress - have no it!

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    // Never called
}

-(void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event {
    // Works fine!
}

Answer

lemnar picture lemnar · Sep 13, 2015

Presses are related to physical buttons, like the Menu button. A press begins when you start holding down such a button, and it ends when you stop holding down the button. There isn't any screen-relative position associated with a press.

Touches in tvOS are similar to touches in iOS, but there's one important difference: they're "indirect" touches, i.e. there isn't a physical relationship between the location of the finger and a location on screen.

When a touches begins, it will be delivered to the focused view, and the touch will be considered to have started in the center of that view, regardless of the absolute position of the finger on the touch surface. As the touch moves, its screen-relative position will be updated accordingly.

I'm not aware of any API that would allow you to determine the absolute position of a finger on the touch surface.

In your case, making your responder the focused view should cause it to receive touch events.