Is creating a button slower than creating Text?

Viewed 55

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.

1 Answers

Normally there is always a competition between drag gesture (means items use drag gesture like: Button, Slider, Toggle, . .) and scrolling functionality (means items you can scroll: ScrollView, List, Picker, . .) and it is better always use Text with on tap gesture because it doesn't compete with scrolling on drag gesture.

Button use drag gesture but Text does not, there you would see better Performance with Text.

Related