I have a UIViewRepresentable view that is put up by SwiftUI, in which I create three or four fairly complex graphic objects, and move them within the view at a precise speed. I use a CADisplayLink to calculate how much these objects should move for every new frame.
My frame rate wasn't stellar, so I thought that I would try to cache chunks of the image by creating a UIImage programatically, and caching it in main memory. This works well enough, and I can create new UIImage objects in a separate thread, but the frame rate is actually a bit slower than just drawing everything as it's needed.
The view contains three or four of these things, and I have a background task that creates the UIIMages that will be required in the near future, and re-uses UIImages that have scrolled off the screen, so my memory utilization is relatively minimal - if necessary, I could create one big composite image that just gets scrolled around programatically using a ScrollView, but I am concerned that this approach would use significantly more memory.
Is there a better way to cache reusable chunks of a view and animate them across the view? I would like to offload as much as possible to the GPU, of course. Is creating a UIImage the right approach? Would I be better off creating a CALayer, or some other bitmap representation? Should I put these things into a ScrollView? Should I be creating a view that can be animated by the system instead of animating it frame by frame using CADisplayLink timing? (I need to be able to cancel the animation at a moment's notice).
I appreciate that this might not be a simple code issue that only has one correct answer, but I'd appreciate a push in the right direction, thanks!