|
I want the "Play/Pause" button to animate when I tap the "Reset" button.
|
|
Excellent!This is a great effort. We can see your logic, and how you are thinking about this solution. Well done. You ask about animation, but taking hammers and screwdrivers to address your question may not be the right answer for you. There are some other issues in your solution that, when addressed, make the animation part much simpler. First, consider this variable Take a step back and think about the state of your views. When I drew your app on a blank sheet of paper, I considered you had two different states:
You can see how you can mix and match these two states:
You are trying to ask your buttons to draw themselves based on one state named: isButtonTapped. But your application has three states. Please consider removing
This is much more “swifty” in that it allows you to be more declarative in your code. You can use these two states to TELL SWIFTUI how to look when either of these two states are true or false! Sweet! Also, please consider adding these two Text() views to the top of your
These will help you understand the state each of your views is in. Then you can easily decide on the rules. You can think to yourself, when my application is running, and the timer is paused, I want this button to be RED, and the text to say Paused. Or you can say I want this button to HIDE and this button to be WIDER when the application state is RESET. It’s easy for you to see the different states and make notes what you should see when the states change. THIS leads to your question. HOW do you ask the views to animate? The answer is with the How do you know when this change will happen? It happens whenever your state variables change! And now you can clearly see them at the top of your VStack. So when the
Full solution:
|
|
Other Notes:Consider making your buttons DECLARE what you want them to do. Easy! The reset button should stop the timer, and tell all views to reset themselves to their starting positions. So extract all that code into a view function and name it resetTheTimer(). Then your button is much simpler to manage. Do the same with the Pause or Play button. Extract all the logic out of the Button code, and create a separate function to handle these tasks.
|
|
Other notes:In your code, Avoid writing code like this:
Let your Swift code read like English.
|
|
Other notes:Let Swift worry about details. You had a variable in your code named
This was frightenly messy because you had to manually think about the state of your appliction and the state of your timer and then set this flag to TRUE or FALSE based on your application's rules. You had to do this in a few places. But why bother yourself with this detail? 100% chance you'll mess this up in some future application and we'll read about how you sent a Mars rover off course, or accidentally ordered 100 cups of coffee instead of 1. You have a variable that holds the timer value named If you need a boolean to tell you if the timer is greater than zero, consider using a computed property.
This is way better! Now you are no longer in the business of trying to keep this variable in sync with your currentTime variable. You can delete all the code in your view where you set or reset this variable. Instead, it will always reflect the true state of your Ask for Feedback!There are other refinements you could make. These are a few for you to consider. Keep asking for feedback, though! This is the value of working with a team of programmers. Code Reviews are a critical function of a programmer's environment. |
|
One more consideration.I suggested your application has two states to track.
Then the solution was created using BOOLEANS to track these two different state variables. Maybe you can find an elegant solution using only ONE state variable to track all your application's states? But instead of a BOOLEAN, you may have to consider using an enum to define your application's state. Think about it and drop your thoughts here. |
|
Hey @Obelix, First of all, I want to thank you for your super extensive answers and constructive criticism. I don't know if this is standard in this forum, but I appreciate it. To be honest, I didn't get any thought from you (14-year-old teaching myself Swift for 2 months yet). I try to dive deep into your responses and hope I get to understand every bit. |
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.