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

SOLVED: Animations - Strange Behavior when Calling Animations in ContentView

Forums > 100 Days of SwiftUI

I'm working through this example that Paul posted:

struct ContentView: View {
    let letters = Array("Hello SwiftUI")
    @State private var enabled = false
    @State private var dragAmount = CGSize.zero

    var body: some View {
        HStack(spacing: 0) {
            ForEach(0..<letters.count) { num in
                Text(String(letters[num]))
                    .padding(5)
                    .font(.title)
                    .background(enabled ? .blue : .red)
                    .offset(dragAmount)
                    .animation(.default.delay(Double(num) / 20), value: dragAmount))
            }
        }
        .gesture(
            DragGesture()
                .onChanged { dragAmount = $0.translation }
                .onEnded { _ in
                    dragAmount = .zero
                    enabled.toggle()
                }
        )
    }
}

This is supposed to create a snake-like animation with the characters in the string.

I wrote this code in a separate Swift View called "SnakeTextAnimation", but when I try to instantiate this struct within ContentView with SnakeTextAnimation(), it doesn't produce the expected resut. It changes colors, but it doesn't move.

Why does this only work in ContentView, but not when I call it from another view?

Xcode Project: https://github.com/iosPit/CodeSnippets -> Animations Folder

2      

You missing .offset(dragAmount)

struct SnakeTextAnimation: View {
    let snakeText = Array("Snake Text Animation")
    @State private var isEnabled = false
    @State private var dragAmount = CGSize.zero

    var body: some View {
        HStack(spacing: 0) {
            ForEach(0..<snakeText.count) { num in
                Text(String(snakeText[num]))
                    .padding(1)
                    .font(.title)
                    .background(isEnabled ? .blue : .red)
                    .offset(dragAmount)
                    .animation(.default.delay(Double(num)/20), value: dragAmount)
            }
        }
        .gesture(
            DragGesture()
                .onChanged { dragAmount = $0.translation }
                .onEnded { _ in
                    dragAmount = .zero
                    isEnabled.toggle()
                }
        )
    }
}

Works fine then

2      

Thank you, Appreciate the help !:D

2      

@Obelix, I think Nigel went into my Github repository to figure out what I was missing. The code I pasted is from the article.

I thought I had written the code in the article correctly into my XCode, but I missed the .offset(dragAmount)

-Pit

2      

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.