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

Animation Challenge

Forums > 100 Days of SwiftUI

Hi! I'm trying to make "plus score" animation. When answering correct, "+1" animation appears and scales towards user, then dissappears, when next question asked.

Here's the code I've tried. The problem here is that animation flies away, instead of flying towards. How to fix it? Also, let's try together to improve the animation?!

import SwiftUI

struct PlusOneAnimation: View {
    @State private var score = 0
    @State private var showPlusScore = false
    @State private var animationAmount: CGFloat = 1

    var body: some View {
        VStack {
            //Text(showPlusScore ? "+1" : "") - this way didn't work for me
            // note - if I used either .animation( ) modifier or withAnimation
            // separately, it didn't work. 
            // Only using both - .animation( ) and withAnimation( ) { ... }  together,
            // worked out to animate
            Text("+1")
                .scaleEffect(animationAmount)
                .opacity(showPlusScore ? 1 : 0)
                .animation(.easeOut(duration: 0.5))
                .padding(50)

            Text("Score: \(score)")
                .padding(.bottom, 40)

            Button("Correct Answer") {
                score += 1
                withAnimation(.easeOut(duration: 0.5)) {
                    showPlusScore = true
                    animationAmount += 4
                }

                newRound()
            }
            .padding(.bottom)

            Button("Wrong Answer") {

            }
        }
    }

    func newRound() {
        showPlusScore = false

        // I've tried to make some delay, but it didn't work
        withAnimation(Animation.easeInOut(duration: 0.5).delay(3)) {
            animationAmount = 1
        }

    }
}

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.