ios – How to prevent SwiftUI Scrollview from bottlenecking performance of Canvas


When using ScrollView and Canvas together, every scroll event by the scrollview causes a redraw, which is a heavy bottleneck on my view’s performance, which utilizes a canvas for ~100×100 grid. Is there a way to prevent, or at least mitigate this constant redrawing for better canvas scrolling performance? It looks somewhat choppy when scrolling on very complicated canvas views, which doesn’t happen in HTML.
A simple example showing that ScrollView causes lots of redraws:

import SwiftUI
var i = 0
struct ContentView: View {
    var body: some View {
        ScrollView([.horizontal, .vertical]) {       
            Canvas { _, _ in
                i = i+1
                print(i)
                
            }.background(.red).frame(width: 2000, height: 2000, alignment: .center)
        }
    }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img