TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to prevent SwiftUI Scrollview from bottlenecking performance of Canvas

Forums > SwiftUI

I asked this same question on StackOverflow, but I have not had a response yet, so I am going to ask about this problem here.

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 ~100x100 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.

This behavior seems to only happen with large canvases, as a ScrollView with many small Canvases do not replicate this same behavior of constant redrawing.

Here is 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)
        }
    }
}

I do not believe a solution with Shape would be benifitial due to performance issues, and the overhead with using a GPU shader would be problematic with text. I need a solution that works with iOS 15, as I am developing for older devices.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.