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

Animation .animation deprecated

Forums > SwiftUI

Maybe I should just move along because the code is working but I have so many questions where I don't understand.

So, objective is to just have an image rotating.

Code I found informed me that .animation has been deprecated.

So here's some stuff I discovered that doesn't make sense to me.

This code works, but (1). why do I have to do onAppear? (without onAppear the image doesn't rotate).

   @State private var isRotating = true

    var body: some View {
        ZStack {
            Image("Layer0")
                .resizable()
                .frame(width: 100, height: 100)
                .rotationEffect(.degrees(isRotating ? 360 : 0))
            //                .animation(Animation.linear(duration: 1).repeatForever(), value: isRotating)
                .animation(.linear(duration: 10).repeatForever(autoreverses: false), value: isRotating)
        }
        .onAppear {
                    isRotating = false
                }
    }

Then, this is crazy to me, if I change the var to false and the isRotating to true it just reverses from clockwise to counter-clockwise. (2). I would think saying isRotating = false would mean No animation. Why does it reverse the direction?

2      

Thanks @nigelgee , this is helpful. Earlier today I had to comment out this code to make the animation work. animation(Animation.linear(duration: 10).repeatForever(autoreverses: false), value: isRotating) Basically, I deleted Animation, and used this code to make it work: .animation(.linear(duration: 10).repeatForever(autoreverses: false), value: isRotating)

Now, for no reason I can deduce, both work. Is that just Xcode catching up? Or inevitable weirdness?

My question in this case is basically, is the word Animation assumed in the second version of that line of code? Anyway, I'm still confused, even after reading the page you shared, about why: onAppear { isRotating = false } would make it rotate in reverse as opposed to making it NOT rotate.

I just spent three hours reading https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID330 and https://docs.swift.org/swift-book/GuidedTour/GuidedTour.html and boy oh boy does it make me appreciate you and others who respond to my specific challenges on this journey. All the official docs, even from the first paragraph assume the reader is coming from a background knowing Objective C.

If anyone knows a version of Swift and SwiftUI docs that assume zero Objective C background, I'd love to know about that! Thanks.

2      

Another question, after some research, it seems I can't apply a .rotationEffect to a View like my ZStack here. Is that true? If so, will that help me understand Views or modifiers better?

struct _2animation: View {

    @State private var isRotating = false

    var body: some View {
        ZStack {
            Image("Layer0")
                .resizable()
                .frame(width: 100, height: 100)
        }
        .rotationEffect(.degrees(isRotating ? 360 : 0))
        .animation(.linear(duration: 10).repeatForever(autoreverses: false), value: isRotating)

        .onAppear {
            isRotating = true
        }
    }
}

in the above case, the image does not rotate.

I guess my question is how would I know what can and can't be modified by this modifier and what other modifiers are in the same category (not modifying)?

2      

Gabe, do you understand what this part means? isRotating ? 360 : 0

If not, review https://www.hackingwithswift.com/sixty/3/7/the-ternary-operator followed by https://www.hackingwithswift.com/books/ios-swiftui/conditional-modifiers

Once you understand that, you'll understand why it's rotating that way.

2      

Holy wow. Thank you for you sharing those two links. When you put it that way it's totally obvious. But then I get stuck in tutorials differentiating Swift commands versus variables or other things that I name myself.

The variable being isRotating I took literally.

In fact, isRotating is boolean, on or off. When it's on (true) the image goes to 360 degrees, and when it's off (false) it goes to 0 degrees.

It just seems like repeatForever and even rotationEffect are shortcuts and following them to documentation, these methods aren't showing me how to they actually work.

2      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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.