tvOS swipe left/right gesture support (patch enclosed)

"oviano" <[email protected]> Sun, 18 Dec 2016 12:15:56 +0000
Newsgroups gmane.comp.lib.sdl
Message-ID <[email protected]>
I found that events weren't being generated for the simple directional swipes on the Apple TV touch remote.

While it's possible to use the touch press/release/motion events to detect simple swipes of this nature (and I had this working well enough) it struck me that this was re-inventing the wheel.

As such, I have written a patch that recognises the proper swipe up/down/left/right events issued in UIKit and passes these on as directional key presses. This also means that the touch remote operates the same way as the old-style IR remote in this respect.


Code:

diff -r 007dfe83abf8 src/video/uikit/SDL_uikitview.m
--- a/src/video/uikit/SDL_uikitview.m	Wed Oct 19 20:50:33 2016 -0700
+++ b/src/video/uikit/SDL_uikitview.m	Sun Dec 18 14:57:42 2016 +0300
@@ -48,7 +48,20 @@
 #if !TARGET_OS_TV
         self.multipleTouchEnabled = YES;
 #endif
-
+#if TARGET_OS_TV
+        UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
+        swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
+        [self addGestureRecognizer:swipeUp];
+        UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
+        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
+        [self addGestureRecognizer:swipeDown];
+        UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
+        swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
+        [self addGestureRecognizer:swipeLeft];
+        UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
+        swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
+        [self addGestureRecognizer:swipeRight];
+#endif
         touchId = 1;
         SDL_AddTouch(touchId, "");
     }
@@ -56,6 +69,27 @@
     return self;
 }
 
+#if TARGET_OS_TV
+- (void)swipeGesture:(UISwipeGestureRecognizer *)sender
+{
+    switch (sender.direction)
+    {
+    case UISwipeGestureRecognizerDirectionUp:
+        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_UP);
+        break;
+    case UISwipeGestureRecognizerDirectionDown:
+        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DOWN);
+        break;
+    case UISwipeGestureRecognizerDirectionLeft:
+        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LEFT);
+        break;
+    case UISwipeGestureRecognizerDirectionRight:
+        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RIGHT);
+        break;
+    }
+}
+#endif
+
 - (void)setSDLWindow:(SDL_Window *)window
 {
     SDL_WindowData *data = nil;

_______________________________________________
SDL mailing list
[email protected]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org