Last active
May 10, 2024 09:35
-
-
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.
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
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) | |
} | |
} |
Author
theoknock
commented
May 10, 2024

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