Search code examples
tvosuipangesturerecognizerapple-tvbluetooth-keyboard

Bluetooth keyboard not responding to UIPanGestureRecognizer on Apple TV


I am getting some issue with using a bluetooth keyboard to UIPanGestureRecognizer which is used for scrolling a textview. Do I need a different recognizer to detect up and down arrows?


Solution

  • If you're wanting to scroll a text view, the text view already has a built-in gesture that listens for keyboard events (or any other UIPress event), you just need to enable it.

    textView.directionalPressGestureRecognizer.enabled = YES;
    

    Same thing for using touches to scroll it: the text view's built-in pan gesture can recognize those touches, but it doesn't by default on tvOS. That's because the normal use-case on TV is for touches to move focus, not scroll the scroll view. You can get the pan gesture to listen to touches by changing it's allowedTouchTypes like so:

    textView.panGestureRecognizer.allowedTouchTypes = @[ @(UITouchTypeIndirect) ];