问题
I know, SwiftUI is currently in beta, but i want to be sure to use focusable correctly.
Actually, when i scroll down in the list, the focused item skips 3 or 4 rows when the list scroll down (up scroll direction is fine).
You can try with this code, in b4 version: (Edit: same for b5)
import SwiftUI
struct TestList: Identifiable {
var id: Int
var name: String
}
let testData = [Int](0..<50).map { TestList(id: $0, name: "Row \($0)") }
struct SwiftUIView : View {
var testList: [TestList]
var body: some View {
List {
ForEach(testList) { txt in
TestRow(row: txt)
}
}
}
}
struct TestRow: View {
var row: TestList
@State private var backgroundColor = Color.clear
var body: some View {
Text(row.name)
.focusable(true) { isFocused in
self.backgroundColor = isFocused ? Color.green : Color.blue
if isFocused {
print(self.row.name)
}
}
.background(self.backgroundColor)
}
}
#if DEBUG
struct SwiftUIView_Previews : PreviewProvider {
static var previews: some View {
SwiftUIView(testList: testData)
}
}
#endif
Thanks.
回答1:
It finally working for me with Xcode 11 GM...
来源:https://stackoverflow.com/questions/57129603/swiftui-and-tvos-focusable