Skip to content

Instantly share code, notes, and snippets.

@aessam
Created February 17, 2021 00:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aessam/af37e980d91d23b6b992f992e7fd0c1a to your computer and use it in GitHub Desktop.
Save aessam/af37e980d91d23b6b992f992e7fd0c1a to your computer and use it in GitHub Desktop.
Drawing segments of a circle in SwiftUI
import SwiftUI
extension Color {
static var random: Color {
return Color(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
}
}
struct ContentView: View {
@State var sections: Double = 3
var body: some View {
Slider(value: $sections, in: 2...20, step: 1)
ZStack {
ForEach(0..<20) { index in
Circle()
.trim(from: 0.017, to: (1/CGFloat(sections)) - (0.017))
.rotation(.degrees(Double(CGFloat(index) * CGFloat(360/sections))))
.rotation(.degrees(-90))
.stroke(Color.random ,style: StrokeStyle(lineWidth: 50, lineCap: .round))
.frame(width: 520, height: 520)
}
Circle()
.frame(width: 425, height: 425)
Text("")
.font(.system(size: 420, weight: .light, design: .serif))
.foregroundColor(.black)
}.padding(50)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment