UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Project 6 - Day 32: Strange animation behavior when using animation modifier inside the Stepper

Forums > 100 Days of SwiftUI

Hi folks,

I am following the 100 Days of SwiftUI challenge and for the Animating Bindings explanation I got a strange animation behavior when applying the same code of the presentation.

My full code is:

import SwiftUI

struct ContentView: View {

    @State private var animationAmount: CGFloat = 1

    var body: some View {

        print(animationAmount)

        return VStack {

            Stepper("Scale amount", value: $animationAmount.animation(
                Animation.easeInOut(duration: 1)
                .repeatCount(3, autoreverses: true)
            ), in: 1...10)

            Spacer()

            Button("Tap here!") {
                self.animationAmount += 1
            }
            .padding(40)
            .background(Color.red)
            .foregroundColor(Color.white)
            .clipShape(Circle())
            .scaleEffect(animationAmount)
        }

    }
}

Everything goes fine if I use the Stepper view as Stepper("Scale amount", value: $animationAmount.animation(), in: 1...10) and the animationAmount reaches a limit of 10 and I can't increase the button size. But if I run the above mentione code (adding modifiers inside the Stepper view) and press the + button quickly I can have animationAmount values greater than 10 (even specifying the range in: 1...10). Am I missing something here?

I am running Xcode Version 11.5 (11E608c) on Catalina 10.15.5.

3      

I just ran your code on a new Xcode project on my mac. What happened to you doesn't happen to me. Stepper doesn't go beyond 10. The only way animationAmount can go beyond 10 is by pressing the button.

Are you sure you're not accidentially pressing the button whilst the button hinders the + on the stepper during animation?

If you go beyond 10 by pressing the button, stepper will allow you to press both +/- , animationAmount will be restored back to 10 despite pressing either +/-

If you stop quickly pressing + on the stepper after going beyond 10, wait for 2 seconds and press + again, does it bring animationAmount back down to 10?

I don't think that answers your question on why the stepper allows you go beyond 10 by clicking + quickly. It didn't happen on my Mac.

3      

Thank you very much @lchung678 . I just compiled the code again and the strange behavior has gone... Now works exactly as you described. I think I need to reduce caffeine... hahaha

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.