Last active
December 26, 2020 10:16
-
-
Save CodeSlicing/17a52dfa3cb8d2600bee50f3b0fecca1 to your computer and use it in GitHub Desktop.
Source for part 1 of large sticky toggles CodeSlicing episode
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
// | |
// StickyTogglePhase1.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 14/12/2020. | |
// Copyright © 2020 Adam Fordyce. All rights reserved. | |
// | |
import SwiftUI | |
import PureSwiftUI | |
private let toggleLayoutConfig = LayoutGuideConfig.grid(columns: 10, rows: 10) | |
private struct StickyToggle: View { | |
var body: some View { | |
let debug = false | |
StretchableSquare(stretchFactor: 0, isTop: true, debug: debug) | |
.toggleStyle(color: .green, debug: debug) | |
.frame(360) | |
.shadowIfNot(debug, radius: 10) | |
.layoutGuide(toggleLayoutConfig) | |
.showLayoutGuides(debug) | |
} | |
} | |
private extension Shape { | |
@ViewBuilder func toggleStyle(color: Color, debug: Bool = false) -> some View { | |
if debug { | |
stroke(Color.black, style: .init(lineWidth: 2, lineCap: .round, lineJoin: .round)) | |
} else { | |
fill(color) | |
} | |
} | |
} | |
private struct StretchableSquare: Shape { | |
var isTop: Bool | |
var debug: Bool | |
var animatableData: CGFloat | |
init(stretchFactor: CGFloat, isTop: Bool, debug: Bool = false) { | |
animatableData = stretchFactor | |
self.isTop = isTop | |
self.debug = debug | |
} | |
func path(in rect: CGRect) -> Path { | |
var path = Path() | |
let g = toggleLayoutConfig.layout(in: rect) | |
.scaled(-1, factor: isTop ? 0 : 1) | |
let p1 = g.topLeading | |
let p2 = g.topTrailing | |
let cpToP3 = g[10, 1].to(g[6, 1], animatableData) | |
let p3 = g.bottomTrailing.to(g[6, 10], animatableData) | |
let p4 = g.bottomLeading.to(g[4, 10], animatableData) | |
let cpToP1 = g[0, 1].to(g[4, 1], animatableData) | |
path.move(p1) | |
path.line(p2) | |
path.quadCurve(p3, cp: cpToP3, showControlPoints: debug) | |
path.line(p4) | |
path.quadCurve(p1, cp: cpToP1, showControlPoints: debug) | |
return path | |
} | |
} | |
struct StickyTogglePhase1_Previews: PreviewProvider { | |
struct StickyTogglePhase1_Harness: View { | |
var body: some View { | |
StickyToggle() | |
} | |
} | |
static var previews: some View { | |
StickyTogglePhase1_Harness() | |
.previewDevice(.iPhone_8_Plus) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment