|
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. |
|
GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios. 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! |
GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.
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.