|
1 |
| -# swiftui-stagger-animation |
| 1 | +# Stagger |
| 2 | + |
| 3 | +A SwiftUI library for creating beautiful staggered animations with minimal code. |
| 4 | + |
| 5 | +This project is based on the [objc.io Swift Talk episode "Staggered Animations Revisited"](https://talk.objc.io/episodes/S01E443-staggered-animations-revisited). |
| 6 | + |
| 7 | +[](https://swift.org) |
| 8 | +[](https://developer.apple.com/ios) |
| 9 | +[](https://developer.apple.com/macos) |
| 10 | +[](https://developer.apple.com/tvos) |
| 11 | +[](https://opensource.org/licenses/MIT) |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- 🌊 **Simple API**: Add staggered animations with just a single view modifier |
| 16 | +- 🔄 **Customizable**: Control animation timing, order, and transitions |
| 17 | +- 📱 **Accessibility**: Respects reduced motion settings |
| 18 | +- 🧩 **Composable**: Works with any SwiftUI transition |
| 19 | +- 🔍 **Smart sorting**: Order by position, priority, or custom criteria |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +### Swift Package Manager |
| 24 | + |
| 25 | +Add the following to your `Package.swift` file: |
| 26 | + |
| 27 | +```swift |
| 28 | +dependencies: [ |
| 29 | + .package(url: "https://github.com/ivan-magda/swiftui-stagger-animation.git", from: "1.0.0") |
| 30 | +] |
| 31 | +``` |
| 32 | + |
| 33 | +Or add it in Xcode: |
| 34 | +1. Go to File → Add Packages... |
| 35 | +2. Paste the repository URL: `https://github.com/ivan-magda/swiftui-stagger-animation.git` |
| 36 | +3. Click "Add Package" |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +### Basic Usage |
| 41 | + |
| 42 | +```swift |
| 43 | +VStack { |
| 44 | + ForEach(items) { item in |
| 45 | + ItemView(item: item) |
| 46 | + .stagger() // Default opacity transition |
| 47 | + } |
| 48 | +} |
| 49 | +.staggerContainer() // Required to coordinate animations |
| 50 | +``` |
| 51 | + |
| 52 | +### Custom Transitions |
| 53 | + |
| 54 | +```swift |
| 55 | +// Single transition |
| 56 | +Text("Hello").stagger(transition: .move(edge: .leading)) |
| 57 | + |
| 58 | +// Combined transitions |
| 59 | +Image(systemName: "star") |
| 60 | + .stagger(transition: .scale.combined(with: .opacity)) |
| 61 | +``` |
| 62 | + |
| 63 | +### Controlling Animation Order |
| 64 | + |
| 65 | +```swift |
| 66 | +// Setting animation priority (higher values animate first) |
| 67 | +Text("First").stagger(priority: 10) |
| 68 | +Text("Second").stagger(priority: 5) |
| 69 | +Text("Third").stagger(priority: 0) |
| 70 | + |
| 71 | +// Configure stagger container |
| 72 | +VStack { |
| 73 | + // Your views... |
| 74 | +} |
| 75 | +.staggerContainer( |
| 76 | + configuration: StaggerConfiguration( |
| 77 | + baseDelay: 0.1, // Time between each item |
| 78 | + animationCurve: .spring(response: 0.5), |
| 79 | + calculationStrategy: .priorityThenPosition(.topToBottom) |
| 80 | + ) |
| 81 | +) |
| 82 | +``` |
| 83 | + |
| 84 | +### Animation Strategies |
| 85 | + |
| 86 | +```swift |
| 87 | +// Available calculation strategies |
| 88 | +.priorityThenPosition(.leftToRight) // Default |
| 89 | +.priorityOnly |
| 90 | +.positionOnly(.topToBottom) |
| 91 | +.custom { lhs, rhs in |
| 92 | + // Your custom sorting logic |
| 93 | +} |
| 94 | + |
| 95 | +// Available directions |
| 96 | +.leftToRight |
| 97 | +.rightToLeft |
| 98 | +.topToBottom |
| 99 | +.bottomToTop |
| 100 | +``` |
| 101 | + |
| 102 | +### Animation Curves |
| 103 | + |
| 104 | +```swift |
| 105 | +// Available animation curves |
| 106 | +.default |
| 107 | +.easeIn |
| 108 | +.easeOut |
| 109 | +.easeInOut |
| 110 | +.spring(response: 0.5, dampingFraction: 0.8) |
| 111 | +.custom(Animation.interpolatingSpring(mass: 1, stiffness: 100, damping: 10)) |
| 112 | +``` |
| 113 | + |
| 114 | +## Example |
| 115 | + |
| 116 | +```swift |
| 117 | +struct ContentView: View { |
| 118 | + @State private var isVisible = false |
| 119 | + |
| 120 | + let colors: [Color] = [.red, .orange, .yellow, .green, .blue, .purple] |
| 121 | + |
| 122 | + var body: some View { |
| 123 | + VStack(spacing: 16) { |
| 124 | + Text("Stagger Animation Demo") |
| 125 | + .font(.largeTitle) |
| 126 | + .stagger( |
| 127 | + transition: .move(edge: .top).combined(with: .opacity), |
| 128 | + priority: 10 |
| 129 | + ) |
| 130 | + |
| 131 | + LazyVGrid(columns: [GridItem(.adaptive(minimum: 80))], spacing: 16) { |
| 132 | + ForEach(colors.indices, id: \.self) { index in |
| 133 | + RoundedRectangle(cornerRadius: 12) |
| 134 | + .fill(colors[index]) |
| 135 | + .frame(height: 80) |
| 136 | + .stagger(transition: .scale.combined(with: .opacity)) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + Button("Reset") { |
| 141 | + isVisible.toggle() |
| 142 | + } |
| 143 | + .padding() |
| 144 | + } |
| 145 | + .padding() |
| 146 | + .staggerContainer( |
| 147 | + configuration: StaggerConfiguration( |
| 148 | + baseDelay: 0.08, |
| 149 | + animationCurve: .spring(response: 0.6) |
| 150 | + ) |
| 151 | + ) |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +## Requirements |
| 157 | + |
| 158 | +- iOS 17.0+ / macOS 14.0+ / tvOS 17.0+ |
| 159 | +- Swift 6.0+ |
| 160 | +- Xcode 15.0+ |
| 161 | + |
| 162 | +## Contributing |
| 163 | + |
| 164 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 165 | + |
| 166 | +## License |
| 167 | + |
| 168 | +Stagger is available under the MIT license. See the LICENSE file for more info. |
0 commit comments