|
| 1 | +#if canImport(AndroidSwiftUI) |
| 2 | +import AndroidSwiftUI |
| 3 | +#else |
| 4 | +import SwiftUI |
| 5 | +#endif |
| 6 | + |
| 7 | +struct AnimationPlayground: View { |
| 8 | + @State private var moved = false |
| 9 | + @State private var faded = false |
| 10 | + @State private var grown = false |
| 11 | + @State private var recolored = false |
| 12 | + @State private var crawled = false |
| 13 | + var body: some View { |
| 14 | + ScrollView { |
| 15 | + VStack(alignment: .leading, spacing: 0) { |
| 16 | + Example("withAnimation: offset") { |
| 17 | + VStack(alignment: .leading, spacing: 8) { |
| 18 | + Circle() |
| 19 | + .fill(.blue) |
| 20 | + .frame(width: 44, height: 44) |
| 21 | + .offset(x: moved ? 220 : 0, y: 0) |
| 22 | + Button(moved ? "Slide back" : "Slide right") { |
| 23 | + withAnimation { moved.toggle() } |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + Example("withAnimation: slow motion (2s linear)") { |
| 28 | + VStack(alignment: .leading, spacing: 8) { |
| 29 | + Rectangle() |
| 30 | + .fill(.red) |
| 31 | + .frame(width: 44, height: 44) |
| 32 | + .offset(x: crawled ? 220 : 0, y: 0) |
| 33 | + Button(crawled ? "Crawl back" : "Crawl right") { |
| 34 | + withAnimation(.linear(duration: 2)) { crawled.toggle() } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + Example("withAnimation: opacity & scale") { |
| 39 | + VStack(alignment: .leading, spacing: 8) { |
| 40 | + Text("Watch me") |
| 41 | + .padding() |
| 42 | + .background(Color.purple) |
| 43 | + .cornerRadius(8) |
| 44 | + .opacity(faded ? 0.2 : 1) |
| 45 | + .scaleEffect(faded ? 0.7 : 1) |
| 46 | + Button(faded ? "Restore" : "Fade & shrink") { |
| 47 | + withAnimation(.easeOut(duration: 0.6)) { faded.toggle() } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + Example("withAnimation: spring frame") { |
| 52 | + VStack(alignment: .leading, spacing: 8) { |
| 53 | + RoundedRectangle(cornerRadius: 10) |
| 54 | + .fill(.green) |
| 55 | + .frame(width: grown ? 260 : 90, height: 44) |
| 56 | + Button(grown ? "Shrink" : "Grow") { |
| 57 | + withAnimation(.spring()) { grown.toggle() } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + Example(".animation(value:) implicit color") { |
| 62 | + VStack(alignment: .leading, spacing: 8) { |
| 63 | + Text("Background eases on its own") |
| 64 | + .padding() |
| 65 | + .background(recolored ? Color.orange : Color.blue) |
| 66 | + .cornerRadius(8) |
| 67 | + .animation(.easeInOut(duration: 0.8), value: recolored) |
| 68 | + Button("Swap color") { recolored.toggle() } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments