[英]onFocusChange not triggered in SwiftUI tvOS
我正在嘗試設置onFocusChange
並在視圖上單擊操作 function。 但是永遠不會調用onFocusChange
。
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .center, spacing: 5) {
ForEach(self.videos, id: \.id) { video in
Button(action: {
print("clicked")
}){
ItemView(vid: video)
.cornerRadius(5).padding(1)
}.focusable(true, onFocusChange: {
hasFocus in
print("focused")
})
}
}
}
如果我在ItemView()
內移動按鈕視圖,則onFocusChange
有效,但單擊操作無效。
問題的目標尚不清楚,但這里有一個使用自定義按鈕樣式管理焦點和按鈕單擊的替代方法的簡單演示。 也許這會有所幫助。
使用 Xcode 12 / tvOS 14(模擬器)測試 - 比較常規按鈕與自定義按鈕
struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.scaleEffect(configuration.isPressed ? 1.2 : 1)
}
}
struct ContentView: View {
@State private var focused = false
var body: some View {
VStack {
Button(action: {
print(">>>> custom button")
}) { LabelView() }.buttonStyle(MyButtonStyle())
Button("Regular Button") {
print(">> regular button")
}
}
}
}
struct LabelView: View {
@Environment(\.isFocused) var focused: Bool
var body: some View {
RoundedRectangle(cornerRadius: 25.0)
.frame(width: 200, height: 100)
.foregroundColor(focused ? .blue : .gray)
.overlay(Text("Title").foregroundColor(.white))
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.