I have a LazyVGrid in a ScrollViewcontaining up to 1000s of items. I started with having a button as item:
ScrollView {
LazyVGrid(columns: columns, spacing: 0) {
ForEach(0..<number, id: \.self) { i in
Button(action: {
self.action(i)
}) {
ZStack {
...
}
}
}
}
}
But that leads to extremly high cpu usage when scrolling (always 97%+) and very "unsmooth" scrolling. When I change it to Text with inTapGesture it is way more performant and cpu only goes up to >90% for a short time.
ScrollView {
LazyVGrid(columns: columns, spacing: 0) {
ForEach(0..<number, id: \.self) { i in
ZStack {
Text().onTapGesture {
self.action(i)
}
...
}
}
}
}
}
Why is that the case? Is it so much harder to render a Button than a Text? I like the "touch" gesture of the button that's why I would prefer a Button, but not if it is so much slower.