我正在尝试实现一个列表,其中我有200或300个元素,并且我想改变悬停事件上文本的颜色。但该应用程序开始显示悬停事件的延迟。检查下面的示例代码:
struct ContentView: View {
var body: some View {
VStack {
ForEach(0...1000, id:\.self) {index in
Element()
}
}
}
}struct Element: View {
@State private var hover = false
var body: some View {
Text("Not a fast hover!")
.foregroundColor(hover ? Color.blue : Color.white)
.onHover {_ in self.hover.toggle()}
}
}

更新:
这似乎提高了反应能力。此外,如果我改变背景,而不是前景色,代码也是反应更快。
struct Element: View {
@State private var hover = false
var body: some View {
ZStack {
Text("Not a fast hover!").foregroundColor(Color.blue)
Text("Not a fast hover!").opacity(hover ? 0 : 1).foregroundColor(Color.white)
}
.frame(width: 200)
.onHover {_ in self.hover.toggle()}
}
}发布于 2019-10-07 22:32:18
解决方案是使用List组件而不是VStack。
https://stackoverflow.com/questions/58260729
复制相似问题