|
Hi! I am trying to animate a button:
This is going to create an animation that will scale out the button and then scale it back to its original size after one simple tap. In addition, it performs a custom action called action(), but that isn't really useful, I just want to be clear. The problem is when I test this and tap the button, it runs very close to what you would expect, but with an additional and unwanted part. When I tap the button, it becomes large and then its original size and then large again, but then it jumps back with no animation to the large size again. I believe that SwiftUI is trying to keep all of the state updated (isPressed), as it starts out false, becomes true when button is tapped, evaluates the ternary in .scaleEffect to be 1.25, which makes it larger. It then reverses this animation to make it small again... but then it must make the size larger again (1.25) because... why? I feel like I am getting confused here. How can I make the button simply get larger and then go back to its original size? What am I misunderstanding here? Please feel free to let me know if there are any clarifications needed. I appreciate the help! :) Hint: Realize that this isn't a SwiftUI Button type, but rather just a custom shape that I made. |
|
One thing to remember about variables. When you set a variable, it is instantly set. That is, when you are animating a scale from 1.0 up to 1.25 the variable instantly jumps to 1.25. It does NOT change from 1.0 to 1.1, then to 1.15, then to 1.20, then to 1.21, etc. So when you set But in your case, you specified TWO animations: small to big, then big back down to small. SwiftUI smoothly animated this for you. But your END state was a big size. So SwiftUI was obligated to draw the final view to your precise specifications. That is, the button label had to be BIG. Since the animation ended in a small state, but you declared the view to be a big state, SwiftUI abruptly changed the text view to be the final large size without the smooth animation. Copy into Playgrounds and give this a spin.
|
|
Hi! Unfortunately, I am away from my computer and cannot test this. However, it looks like this will make the button end up in a bigger size than the original size, but have a smooth animation. I have tried doing just that, but my goal is to create a button that ends up in the original size: you tap the button once, it becomes large and then back to its original size, and then the action happens. I found a person who happened to have the same question as me, but have not had time to look at the answers yet (I just want to clarify and respond to you in a timely manner): https://stackoverflow.com/questions/57252706/animations-triggered-by-events-in-swiftui I will take a look at everything in more detail within the next few hours, thank you for your response! |
|
Try this variation on a theme. I added a short delay that resets the Also, I am a fan of computed properties. It's a way to positively declare your design intentions. Copy into Playgrounds and give this a spin.
|
|
Oh! I didn't think of using DispatchQueue! Thank you, this is a great solution! I ended up making some changes but here is what I ultimately did:
A couple of thoughts:
|
SPONSORED Debug 10x faster with Proxyman. Your ultimate tool to capture HTTPs requests/ responses, natively built for iPhone and macOS. You’d be surprised how much you can learn about any system by watching what it does over the network.
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.