โญ๏ธ ๆณจๆ๏ผ
็ถๆ่ฝ่ขๅน็ๆๅ๏ผไฝฟ็จ 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
}
}
}
}
struct ContentView: View {
var body: some View {
VStack {
Spinner()
Spinner2()
}
.padding(8)
.border(.secondary)
}
}