Skip to content

Instantly share code, notes, and snippets.

@zacharysyoung
Last active August 22, 2020 05:27
Show Gist options
  • Save zacharysyoung/b2c32092eae27b88b7729f83351e9f5d to your computer and use it in GitHub Desktop.
Save zacharysyoung/b2c32092eae27b88b7729f83351e9f5d to your computer and use it in GitHub Desktop.
Developing SwiftUI chops
//
// ContentView.swift
// Dice2
//
// Created by Zach Young on 8/17/20.
// Copyright © 2020 Zach Young. All rights reserved.
//
import SwiftUI
let one = [
[0,0,0],
[0,1,0],
[0,0,0]
]
let two = [
[1,0,0],
[0,0,0],
[0,0,1]
]
let three = [
[1,0,0],
[0,1,0],
[0,0,1]
]
let four = [
[1,0,1],
[0,0,0],
[1,0,1]
]
let five = [
[1,0,1],
[0,1,0],
[1,0,1]
]
let six = [
[1,0,1],
[1,0,1],
[1,0,1]
]
struct ContentView: View {
var body: some View {
VStack {
Spacer()
HStack {
Spacer()
Die(dots: one)
Spacer()
Die(dots: two)
Spacer()
}
Spacer()
HStack {
Spacer()
Die(dots: three)
Spacer()
Die(dots: four)
Spacer()
}
Spacer()
HStack {
Spacer()
Die(dots: five)
Spacer()
Die(dots: six)
Spacer()
}
Spacer()
}
}
}
struct Die: View {
let dots: [[Int]]
var body: some View {
RoundedRectangle(cornerRadius: 10)
.stroke(Color.black, lineWidth: 4)
.overlay(
VStack {
ForEach(0...2, id: \.self) { row in
HStack {
ForEach(self.dots[row], id: \.self) { dotIsShown in
Dot(shown: dotIsShown == 1)
}
}
}
}
)
.aspectRatio(1.0, contentMode: .fill)
.scaleEffect(0.5)
}
}
struct Dot: View {
let shown: Bool
var body: some View {
Circle()
.fill(shown ? Color.black : Color.clear)
.aspectRatio(1.0, contentMode: .fill)
.scaleEffect(0.75)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ForEach(["iPad Pro (12.9-inch) (4th generation)", "iPhone 11 Pro Max"], id: \.self) { deviceName in
ContentView()
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
}
}
//
// ContentView.swift
// AddShapesViews
//
// Created by Zach Young on 8/21/20.
// Copyright © 2020 Zach Young. All rights reserved.
//
import SwiftUI
struct ContentView: View {
@State var circles: [MyCircle] = [MyCircle]()
@State var limitReached: Bool = false
private let objSize: CGFloat = 100.0
var body: some View {
GeometryReader { reader in
ForEach(self.circles, id: \.self) { circle in
circle
.frame(width:self.objSize, height: self.objSize)
.offset(
x: self.calcX(circle.id, reader.size.width),
y: self.calcY(circle.id, reader.size.width)
)
}
Button(action: {
self.circles.append(MyCircle(id: self.circles.count))
let maxCount = Int(
floor(reader.size.width/self.objSize) *
floor(reader.size.height/self.objSize)
)
if self.circles.count == maxCount {
self.limitReached = true
}
}) {
Text("Add circle (\(self.circles.count))")
}
.padding(10)
.position(x: reader.size.width/2, y: reader.size.height - 10)
.disabled(self.limitReached)
}
}
func calcX(_ idx:Int, _ width: CGFloat) -> CGFloat {
return CGFloat(
Double(idx).truncatingRemainder(
dividingBy: Double(floor(width/self.objSize))
)
) * self.objSize
}
func calcY(_ idx:Int, _ width: CGFloat) -> CGFloat {
return CGFloat(
floor(Double(idx) / Double(floor(width/self.objSize)))
) * self.objSize
}
}
struct MyCircle: View, Hashable {
let id: Int
var body: some View {
return Circle()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let devices = [
"iPhone SE",
"iPhone 11",
"iPad Pro (11-inch) (2nd generation)"
]
return ForEach(devices, id: \.self) { name in
ContentView()
.previewDevice(PreviewDevice(rawValue: name))
.previewDisplayName(name)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment