How can you recognise Siri Remote swipe gestures from SwiftUI.
I seems like it has not yet been implemented, so how can I get around that?
How can you recognise Siri Remote swipe gestures from SwiftUI.
I seems like it has not yet been implemented, so how can I get around that?
i have 2 answers for this question, i will answer them separately and let you guys decide which is best,
first is the apple way (which obviously doesn't work allways, there are more clicks captured then swipes):
import SwiftUI
struct SwipeTestView: View
{
var body: some View
{
Text("This can be some full screen image or what not")
.focusable() // <-- this is a must
.onMoveCommand { direction in // <-- this $#!* can't tell a move swipe from a touch (direction is of type: MoveCommandDirection)
print("Direction: \(direction)")
if direction == .left { print(">>> left swipe detected") }
if direction == .right { print(">>> right swipe detected") }
if direction == .up { print(">>> up swipe detected") }
if direction == .down { print(">>> down swipe detected") }
}
}
}
you really (and i can't emphasize this enough) have to swipe on the very edge of the Siri-remote or the iPhone Siri remote widget,
so try to swipe on these yellow areas and try not to tap you'r finger and then swipe, rather gently swipe outwards and let you'r finger get outside of the remote edge completely
Result expected:
i literally tried 100+ times before successfully captured a swipe (clearly not something for production), hopefully tvOS 15 and higher will fix this
the other (more reliable but hacky) way is to use the GameController low-level x/y values for dPad control,
the Siri remote is considered a Game Controller as well, and it is the first one set to the connected game controllers of the apple tv,
so onAppear of a SwiftUI view you can do something like this:
this is a far more reliable solution and works every time, all you have to do is swipe finger from the center of the Siri remote outwards to your desired swipe direction (up / down / left / right),
you could also implement this way up+left, up+right, down+left, down+right, circular-clockwise swipe or circular-counter-clockwise and what ever you want.
You even might be able to implement magnification gesture and alike using the simultaneousGesture()
GCController.controllers().first
will return nil, you need a real hardware to try it, see this answerI wrote several extensions based on that and tested (tvOS 14.7), here is one that you can use as SwipeGesture for tvOS:
and you can use it like this: