Created
February 20, 2021 12:04
-
-
Save CodeSlicing/f97ef600159ba141e823410d3e2a76cd to your computer and use it in GitHub Desktop.
Source code for CodeSlicing episode on Squishy Toggles Part 1
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
// | |
// SquishyTogglePart01.swift | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
// of the Software, and to permit persons to whom the Software is furnished to do so, | |
// subject to the following conditions: | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | |
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
// | |
// Created by Adam Fordyce on 20/02/2021. | |
// Copyright © 2020 Adam Fordyce. All rights reserved. | |
// | |
import SwiftUI | |
import PureSwiftUI | |
private let frameLayoutConfig = LayoutGuideConfig.grid(columns: [0.25, 0.4, 0.6, 0.75], rows: 2) | |
struct SquishyTogglePart01: View { | |
var body: some View { | |
let debug = false | |
GeometryReader { (geo: GeometryProxy) in | |
let size = calculateSize(from: geo) | |
ZStack { | |
ToggleFrame(debug: debug) | |
.styling(color: .green) | |
.layoutGuide(frameLayoutConfig, color: .green, lineWidth: 2) | |
} | |
.frame(size) | |
.borderIf(debug, Color.gray.opacity(0.2)) | |
.greedyFrame() | |
} | |
.showLayoutGuides(debug) | |
} | |
func calculateSize(from geo: GeometryProxy) -> CGSize { | |
let doubleHeight = geo.heightScaled(2) | |
if geo.width < doubleHeight { | |
return CGSize(geo.width, geo.halfWidth) | |
} else { | |
return CGSize(doubleHeight, geo.height) | |
} | |
} | |
} | |
private struct ToggleFrame: Shape { | |
private let debug: Bool | |
init(debug: Bool = false) { | |
self.debug = debug | |
} | |
func path(in rect: CGRect) -> Path { | |
var path = Path() | |
let g = frameLayoutConfig.layout(in: rect) | |
let curveYOffset = rect.heightScaled(0.18) | |
let arcRadius = rect.halfHeight | |
path.move(g[0, 0]) | |
path.curve(rect.top.yOffset(curveYOffset), | |
cp1: g[1, 0], | |
cp2: g[1, 0].yOffset(curveYOffset), | |
showControlPoints: debug) | |
path.curve(g[3, 0], | |
cp1: g[2, 0].yOffset(curveYOffset), | |
cp2: g[2, 0], | |
showControlPoints: debug) | |
path.arc(g[3, 1], radius: arcRadius, startAngle: .top, endAngle: .bottom) | |
path.curve(rect.bottom.yOffset(-curveYOffset), | |
cp1: g[2, 2], | |
cp2: g[2, 2].yOffset(-curveYOffset), | |
showControlPoints: debug) | |
path.curve(g[0, 2], | |
cp1: g[1, 2].yOffset(-curveYOffset), | |
cp2: g[1, 2], | |
showControlPoints: debug) | |
path.arc(g[0, 1], radius: arcRadius, startAngle: .bottom, endAngle: .top) | |
path.closeSubpath() | |
return path | |
} | |
@ViewBuilder func styling(color: Color) -> some View { | |
if debug { | |
strokeColor(.black, lineWidth: 2) | |
} else { | |
fill(color) | |
} | |
} | |
} | |
struct SquishyTogglePart01_Previews: PreviewProvider { | |
struct SquishyTogglePart01_Harness: View { | |
var body: some View { | |
SquishyTogglePart01() | |
.frame(400) | |
} | |
} | |
static var previews: some View { | |
SquishyTogglePart01_Harness() | |
.previewDevice(.iPhone_12_Pro_Max) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment