TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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

3      

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.

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.