|
I have this view below that basically slides in a Rectangle with color after 3.5 seconds. But I'd like to refactor the code to allow for more slides and not have to do an if-else for each. What would be the best way to go around doing that? How would you guys "clean up" this code? I'd start with creating an inner Rectangle view to contain the similar codes but what do I do with the show1/2/3 code? thanks in advance, Jayson
|
|
You could use a enum.
Then use a switch statement and switch on show. In your task set show to the enum case you want to show. |
|
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Updated code implementing switch. I tried using an array approach hoping to just go through each array item and cycling through it. I have a viewArray property I can use but for the life of me, I can't figure out how to have it cycle through each array item and then loop back to the start with .transition(.slide) Can anyone point me to the right direction? I am hoping to have the code work with any number of colors I place in the Enum / array. As of now, I'd have to manually place each case in the switch body. thanks, |
|
Try this:
|
|
|
|
And when the state of the view is reset the view is redrawn and as a result the new color is visible. Some SwiftUI modifiers require a certain order. As I understand is that |
|
|
|
Since the Instead of using a separate
This version of |
|
|
|
Ah, didn't even notice that. Sorry! |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.