Last active
March 4, 2021 17:44
-
-
Save CodeSlicing/5ece347bc8e831f6d3c251ed8bb16afe to your computer and use it in GitHub Desktop.
Native 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
// | |
// SquishyTogglePart01Native.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 | |
private struct SquishyTogglePart01Native: View { | |
var body: some View { | |
let debug = true | |
GeometryReader { (geo: GeometryProxy) in | |
let size = calculateSize(from: geo) | |
ZStack { | |
ToggleFrame(debug: debug) | |
.styling(color: Color.green) | |
} | |
.frame(width: size.width, height: size.height) | |
.border(debug ? Color.gray.opacity(0.2) : Color.clear) | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
} | |
} | |
private func calculateSize(from geo: GeometryProxy) -> CGSize { | |
let doubleHeight = geo.size.height * 2 | |
if geo.size.width < doubleHeight { | |
return CGSize(width: geo.size.width, height: geo.size.width * 0.5) | |
} else { | |
return CGSize(width: doubleHeight, height: geo.size.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 curveYOffset = rect.height * 0.18 | |
let arcRadius = rect.height * 0.5 | |
let quarterX = rect.minX + rect.width * 0.25 | |
let threeQuarterX = rect.minX + rect.width * 0.75 | |
let cpXLeft = rect.minX + rect.width * 0.4 | |
let cpXRight = rect.minX + rect.width * 0.6 | |
let p1 = CGPoint(x: quarterX, y: rect.minY) | |
let p1cp2 = CGPoint(x: cpXLeft, y: rect.minY) | |
let p2 = CGPoint(x: rect.midX, y: rect.minY + curveYOffset) | |
let p2cp1 = CGPoint(x: cpXLeft, y: rect.minY + curveYOffset) | |
let p2cp2 = CGPoint(x: cpXRight, y: rect.minY + curveYOffset) | |
let p3 = CGPoint(x: threeQuarterX, y: rect.minY) | |
let p3cp1 = CGPoint(x: cpXRight, y: rect.minY) | |
let p4cp2 = CGPoint(x: cpXRight, y: rect.maxY) | |
let p5 = CGPoint(x: rect.midX, y: rect.maxY - curveYOffset) | |
let p5cp1 = CGPoint(x: cpXRight, y: rect.maxY - curveYOffset) | |
let p5cp2 = CGPoint(x: cpXLeft, y: rect.maxY - curveYOffset) | |
let p6 = CGPoint(x: quarterX, y: rect.maxY) | |
let p6cp1 = CGPoint(x: cpXLeft, y: rect.maxY) | |
path.move(to: p1) | |
path.addCurve(to: p2, control1: p1cp2, control2: p2cp1) | |
path.addCurve(to: p3, control1: p2cp2, control2: p3cp1) | |
path.addArc(center: CGPoint(x: threeQuarterX, y: rect.minY + arcRadius), radius: arcRadius, startAngle: .degrees(-90), endAngle: .degrees(90), clockwise: false) | |
path.addCurve(to: p5, control1: p4cp2, control2: p5cp1) | |
path.addCurve(to: p6, control1: p5cp2, control2: p6cp1) | |
path.addArc(center: CGPoint(x: quarterX, y: rect.minY + arcRadius), radius: arcRadius, startAngle: .degrees(90), endAngle: .degrees(-90), clockwise: false) | |
path.closeSubpath() | |
return path | |
} | |
@ViewBuilder func styling(color: Color) -> some View { | |
if debug { | |
stroke(Color.black, lineWidth: 2) | |
} else { | |
fill(color) | |
} | |
} | |
} | |
struct SquishyTogglePart01Native_Previews: PreviewProvider { | |
struct SquishyTogglePart01Native_Harness: View { | |
var body: some View { | |
SquishyTogglePart01Native() | |
.frame(width: 400, height: 400) | |
} | |
} | |
static var previews: some View { | |
SquishyTogglePart01Native_Harness() | |
.previewDevice("iPhone 12 Pro Max") | |
.previewDisplayName("iPhone 12 Pro Max") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment