WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

How to create a semi constant background?

Forums > SwiftUI

I am trying to create something similar to an oscilloscope display in my SwiftUI app. This is plotting data that is continuously updated. The background has a grid (graticule - see above). The app is drawing the data using a Path with a background of the graticule and works as expected. Currently the graticule is a bitmap image stored in the app's bundle.

I would like to make the graticule dynamic, in that the spacing of the lines can change based on user input. I would create the grid using another Path (or a Shape).

However, the graticule will change infrequently whereas the data can update several times a second. If I use a path for the graticule, that Path will be created every time the data changes. With the original design using a bitmap, the same bitmap was used over and over. I assume that displaying a bitmap is easier than drawing a path. Is there a way I can "save" the adjusted graticule so that the work of creating the path for it is only done when it needs to change.

Mark

   

You are taking the next big step! Please come back and visit us when you make the big time.

Drawing in iOS is upside down, backwards, and at times like a teenage kiss: sloppy and missing the mark.

I will find the reference, but even @twostraws says in one video he manually rotates the iOS drawing grid so that coordinates make sense. [Note to self: Insert reference here]

My advice?

Make your life a whole lot easier and check out PureSwiftUI. PureSwiftUI is a fully featured drawing library in github. It add a great collection of functions to SwiftUI's abilities to draw to screen. In particular, it enables you layout a graph like view to create drawings using a simple x, y grid system.

I cannot do it justice in this one post. I highly recommend his series on youtube. Search for CodeSlicing and watch his beginner series on Layout Guides in SwiftUI. Start there!

Layout Guides in SwiftUI

   

Also, I would not assume that the graticule will redraw as your data is updated. If you use ZStacks, and draw the graticule using a different path from your oscilloscope's data, it's my understanding that iOS' drawing system is very highly tuned to limit screen updates to just those portions of the screen that actually change.

I think you can use xcode's Instrument tool to prove this to yourself. But someone else will need to share their experience with that tool.

   

Thanks for the replies and the links. I'll check them out. I just watched a @twoStraws video where he draws an arc and tweaked the code so that the arc was drawn with 0 degrees at the top and clockwise working the way one would expect.

That is an interesting supposition SwiftUI being smart enough to not redraw. I had assumed that the entire rectangle of the view would be redrawn if the visibliity or content of any part of the view has changed, so if the data plot covers or uncovers a different portion of the graticule the entire graticule would have to be redrawn. I wonder if SwiftUI is somehow caching the Path so that it doesn't have to recreate the path. I'll have to play with my software and see if I can figure that oue.

I did notice something that may apply here. I had VStack with a couple of Text views. They were connected to @Published properties on different view controllers. Due to a bug in my design, updates to one of the view controllers did not cause the view to update on the screen, but whenever the other Text view updated because its @Published property changed, the first Text view would also update to its latest value. So it seems to me that the entire VStack and its sub-views were being redrawn whenever any of the sub-views of the VStatk updated.

Mark

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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.