โœจSpinner

SwiftUI โŸฉ Animations โŸฉ examples โŸฉ

โญ๏ธ ๆณจๆ„๏ผš

็•ถๆ—‹่ฝ‰่žขๅน•็š„ๆ™‚ๅ€™๏ผŒไฝฟ็”จ implicit animation ็š„ Spinner ๆœƒ็”ข็”Ÿๅฅ‡ๆ€ช็š„ใ€Œ้ฃ„็งปใ€๏ผŒไฝ† explicit animation ๅ‰‡ไธๆœƒโ—๏ธ

๐Ÿ“— ๅƒ่€ƒ๏ผšThinking in SwiftUI, Ch. 6: Animations (p.113)

import SwiftUI

// implicit animation
struct Spinner: View {
    // โญ๏ธ view state
    @State private var animating = false 
    var body: some View { 
        Image(systemName: "rays") 
            // โญ๏ธ spin
            .rotationEffect(animating ? .degrees(360) : .zero) 
            // โญ๏ธ forever (without autoreverse)
            .animation(.linear(duration: 2).repeatForever(autoreverses: false))
            // โญ๏ธ immediately
            .onAppear { self.animating = true }
    }
}

// explicit animation
struct Spinner2: View {
    // โญ๏ธ view state
    @State private var animating = false 
    var body: some View { 
        Image(systemName: "rays")
            .foregroundColor(.orange)
            // โญ๏ธ spin
            .rotationEffect(animating ? .degrees(360) : .zero) 
            // โญ๏ธ immediately
            .onAppear {
                // โญ๏ธ forever (without autoreverse)
                withAnimation(.linear(duration: 2).repeatForever(autoreverses: false)) {
                    self.animating = true 
                }
            }
    }
}

Last updated