Created
March 14, 2025 18:12
-
-
Save Codelaby/a11fd112383b7f2f91e4a77c937bf33c to your computer and use it in GitHub Desktop.
Sunburst in SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Stripes { | |
| static func stripedAngularGradient( | |
| _ colors: [Color], | |
| stripeCount: Int = 2, | |
| center: UnitPoint = .center, | |
| angle: Angle = .zero | |
| ) -> AngularGradient { | |
| let stripeWidth: CGFloat = 1.0 / CGFloat(stripeCount) | |
| var stops: [Gradient.Stop] = [] | |
| for i in 0..<stripeCount { | |
| let location = CGFloat(i) * stripeWidth | |
| let color = colors[i % colors.count] | |
| stops.append(Gradient.Stop(color: color, location: location)) | |
| stops.append(Gradient.Stop(color: color, location: location + stripeWidth)) | |
| } | |
| return AngularGradient( | |
| stops: stops, | |
| center: center, | |
| angle: angle | |
| ) | |
| } | |
| } | |
| // How to use | |
| #Preview { | |
| Rectangle() | |
| .fill( | |
| Stripes.stripedAngularGradient( | |
| [.red, .yellow], | |
| stripeCount: 24, | |
| center: .center, | |
| angle: .degrees(54) | |
| ) | |
| ) | |
| .frame(width: 200, height: 200) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment