SwiftUI and tvOS (focusable)

萝らか妹 提交于 2020-06-12 05:51:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!