Skip to content

Instantly share code, notes, and snippets.

@Codelaby
Created March 14, 2025 18:12
Show Gist options
  • Select an option

  • Save Codelaby/a11fd112383b7f2f91e4a77c937bf33c to your computer and use it in GitHub Desktop.

Select an option

Save Codelaby/a11fd112383b7f2f91e4a77c937bf33c to your computer and use it in GitHub Desktop.
Sunburst in SwiftUI
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