NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Day 32 - Creating explicit animations Questions

Forums > 100 Days of SwiftUI


@State private var animationAmount = 0.0
var body: some View {
    Button("Tap Me") {
        withAnimation(.interpolatingSpring(stiffness: 5, damping: 1)) {
            self.animationAmount += 360
    . background(
    .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


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.


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.