Created
September 10, 2020 15:38
-
-
Save kayleg/9926c5f8d169fe5789916ee1abc8484e to your computer and use it in GitHub Desktop.
Base SwiftUI for CircleLayout exercise
This file contains 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
// This is the completed Circle layout sample. Use CircleLayoutExercise.swift as a starting point if you want to try on your own. | |
import SwiftUI | |
struct MiniCircle : View { | |
var title: String | |
var body: some View { | |
Circle() | |
.strokeBorder(Color.white,lineWidth: 2) | |
.background(Circle().foregroundColor(Color.accentColor)) | |
.frame(width: 50, height: 50) | |
.overlay(Text(title).foregroundColor(.white)) | |
} | |
} | |
struct CircularLayout : View { | |
var radius: CGFloat; | |
var count: Int; | |
var body: some View { | |
let angle = 2.0 / CGFloat(self.count) * .pi | |
return ZStack { | |
ForEach((0...self.count-1), id: \.self) { index in | |
MiniCircle(title: "\(index)") | |
.position(x: self.radius + cos(angle * CGFloat(index) + .pi/3) * self.radius, y: self.radius - sin(angle * CGFloat(index) + .pi/3) * self.radius) | |
} | |
} | |
} | |
} | |
struct ContentView: View { | |
@State private var count: Double = 1 | |
var body: some View { | |
VStack { | |
GeometryReader { geometry in | |
Circle() | |
.foregroundColor(.init(hue: 0, saturation: 0.0, brightness: 0.9)) | |
.frame(width: geometry.size.width, height: geometry.size.width, alignment: .center) | |
.overlay( | |
CircularLayout( | |
radius: (geometry.size.width * 0.75) / 2, | |
count: Int(self.count)) | |
.offset(x: geometry.size.width * 0.125, y: geometry.size.width * 0.125) | |
) | |
} | |
Slider(value: $count, in: 1...16, step: 1).accentColor(.blue).padding(10.0) | |
Spacer(minLength: 10.0) | |
}.background(Rectangle().fill(Color.white)).padding(10.0) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
This file contains 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 MiniCircle : View { | |
var title: String | |
var body: some View { | |
Circle() | |
.strokeBorder(Color.white,lineWidth: 2) | |
.background(Circle().foregroundColor(Color.accentColor)) | |
.frame(width: 50, height: 50) | |
.overlay(Text(title).foregroundColor(.white)) | |
} | |
} | |
struct ContentView: View { | |
@State private var count: Double = 1 | |
var body: some View { | |
VStack { | |
Circle() | |
.foregroundColor(.init(hue: 0, saturation: 0.0, brightness: 0.9)) | |
Slider(value: $count, in: 1...16, step: 1).accentColor(.blue).padding(10.0) | |
Spacer(minLength: 10.0) | |
}.background(Rectangle().fill(Color.white)).padding(10.0) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment