BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

Day 32 - Creating explicit animations Questions

Forums > 100 Days of SwiftUI

Hello,

@State private var animationAmount = 0.0
var body: some View {
    Button("Tap Me") {
        withAnimation(.interpolatingSpring(stiffness: 5, damping: 1)) {
            self.animationAmount += 360
        }
    }
    .padding(50)
    . background(Color.red)
     .foregroundColor(.white)
     .clipShape(Circle())
    .rotation3DEffect(.degrees(animationAmount), axis: (x: 0, y: 1, z: 0))

}

}

This code: Clearly adds 360 degrees to animationAmount on each tap. I expected one full revolution on the first tap, two on the second, three on the third ... etc. Instead it always one full rotation. Similarly, if I use 180 degrees as the animation amount, is always adds 180 degrees. Meaning the first revoution will reverse the direction of the button, and the second one sets it straight again. That's pretty cool - being able to reverse a buton.

But, what if I want to something to spin the once the first time, twice the second, three times the third. Does anyone know of a way to accomplish that?

Would this involve - something we'll cover as we go alone - creating our own modifer/extension and calling that?

Thanks! Mary

1      

The reason you get a single full animation is that you are increasing animationAmount by 360 each time. It does not calculate the total number and rotate accordingly, but instead rotates by the amount increased.

If you want to achieve what you are saying: 1 tap = 1 rotation, 2nd tap = 2 rotations etc... then you will need to track the tap amount. i.e. have a counter which increases by 1 with each tap, and multiply 360 * tapCount this should do the trick.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.