For SwiftUI, I found the .focusable() modifier very useful in making views focusable. However, once focused, I found *no* way of detecting a press on that view. I can find if the play/pause button was selected. I can find if the menu button was selected, but found no method to detect taps and long presses. Why are these not available on tvOS? Is there another way?
Thanks for asking this question.
Our tvOS engineering team is also experiencing difficulties with actions (taps) not being passed through when using .focusable().
Additionally, an alternative approach, utilizing FocusedValue to pass data outside of SwiftUI views (recommended at WWDC) is failing.
We'd appreciate any attention you can give to these tvOS issues as soon as possible, Apple. Thank you.
Our tvOS engineering team is also experiencing difficulties with actions (taps) not being passed through when using .focusable().
Additionally, an alternative approach, utilizing FocusedValue to pass data outside of SwiftUI views (recommended at WWDC) is failing.
We'd appreciate any attention you can give to these tvOS issues as soon as possible, Apple. Thank you.
you can detect press or long press while using focusable, like this:
import SwiftUI
struct TestView: View
{
@State var isFocused: Bool = false
var body: some View
{
Button(action: {
print("Clicked") // simple click
}, label: {
Text("Click Me")
})
.buttonStyle(PlainButtonStyle())
.focusable(true, onFocusChange: { focused in
self.isFocused = focused
})
// long press (hold for at least half a second)
.highPriorityGesture(
LongPressGesture(minimumDuration: 0.5)
.onEnded { _ in
print("Long Pressed ...")
}
)
// click
.onLongPressGesture(minimumDuration: 0.01, perform: {
print("Single Short Click Pressed ...")
})
}
}