Skip to content

Instantly share code, notes, and snippets.

@theoknock
Last active May 10, 2024 09:35
Show Gist options
  • Save theoknock/4e94b6ed8944062f9289ad7ad14cee56 to your computer and use it in GitHub Desktop.
Save theoknock/4e94b6ed8944062f9289ad7ad14cee56 to your computer and use it in GitHub Desktop.
A SwiftUI view consisting of a specified number of Circle() views, each with a diameter matching the standard Slider() "thumb" image (30 points), arranged in a perfect circle with an adjustable radius calculated to space them equidistantly, incorporating additional spacing between each circle.
import SwiftUI
struct CircularArrayView: View {
private let diameter: CGFloat = 30
var numberOfCircles: Int = 12
var spacing: CGFloat = 1.0
// Ensures the number of circles is always even
private var evenNumberOfCircles: Int {
numberOfCircles % 2 == 0 ? numberOfCircles : numberOfCircles + 1
}
private var radius: CGFloat {
(diameter + spacing) / (2 * sin(.pi / CGFloat(evenNumberOfCircles)))
}
var body: some View {
ZStack {
ForEach(0 ..< evenNumberOfCircles, id: \.self) { i in
Circle()
.overlay {
Text("\(Int((Double(i) / Double(evenNumberOfCircles)) * 360.0))")
.font(.footnote).fontWeight(.ultraLight).scaledToFit()
.foregroundStyle(.black)
}
.foregroundStyle(Color(hue: (Double(i) / Double(evenNumberOfCircles)), saturation: 1.0, brightness: 1.0))
.frame(width: diameter, height: diameter)
.offset(x: cos(CGFloat(i) / CGFloat(evenNumberOfCircles) * 2 * .pi - .pi / 2) * radius,
y: sin(CGFloat(i) / CGFloat(evenNumberOfCircles) * 2 * .pi - .pi / 2) * radius)
}
}
}
}
struct CircularArrayView_Previews: PreviewProvider {
static var previews: some View {
CircularArrayView(numberOfCircles: 12, spacing: 1)
}
}
@theoknock
Copy link
Author

Screenshot 2024-05-10 at 5 31 58 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment