Last active
February 22, 2021 17:29
-
-
Save CodeSlicing/055629c0da8d76bd535770508e9e9c5b to your computer and use it in GitHub Desktop.
Beating heart source code for CodeSlicing episode on morphing shapes
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
| // | |
| // AnimatedHeartShapeDemo.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 22/02/2021. | |
| // Copyright © 2020 Adam Fordyce. All rights reserved. | |
| // | |
| import SwiftUI | |
| import PureSwiftUI | |
| private let heartColor = Color(red: 225 / 255, green: 40 / 255, blue: 48 / 255) | |
| private let gridConfig = LayoutGuideConfig.grid(columns: 16, rows: 20) | |
| private typealias Curve = (p: CGPoint, cp1: CGPoint, cp2: CGPoint) | |
| private struct AnimatedHeart: Shape { | |
| var factor: CGFloat | |
| let debug: Bool | |
| init(animating: Bool, debug: Bool = false) { | |
| self.factor = animating ? 1 : 0 | |
| self.debug = debug | |
| } | |
| var animatableData: CGFloat { | |
| get { | |
| factor | |
| } | |
| set { | |
| factor = newValue | |
| } | |
| } | |
| func path(in rect: CGRect) -> Path { | |
| var path = Path() | |
| let g = gridConfig.layout(in: rect) | |
| let p1 = g[0, 6].to(g[2, 6], factor) | |
| let p2 = g[8, 4].to(g[8, 5], factor) | |
| let p3 = g[16, 6].to(g[14, 6], factor) | |
| let p4 = g[8, 20].to(g[8, 19], factor) | |
| var curves = [Curve]() | |
| //c1 | |
| curves.append( | |
| Curve(p2, | |
| g[0, 0].to(g[3, 2], factor), | |
| g[6, -2].to(g[7, 2], factor))) | |
| //c2 | |
| curves.append( | |
| Curve(p3, | |
| g[10, -2].to(g[9, 2], factor), | |
| g[16, 0].to(g[13, 2], factor))) | |
| //c3 | |
| curves.append( | |
| Curve(p4, | |
| g[16, 10].to(g[15, 10], factor), | |
| g[10, 13])) | |
| //c4 | |
| curves.append( | |
| Curve(p1, | |
| g[6, 13], | |
| g[0, 10].to(g[1, 10], factor))) | |
| path.move(p1) | |
| for curve in curves { | |
| path.curve(curve.p, cp1: curve.cp1, cp2: curve.cp2, showControlPoints: debug) | |
| } | |
| return path | |
| } | |
| } | |
| struct AnimatedHeartShapeDemo_Previews: PreviewProvider { | |
| struct AnimatedHeartShapeDemo_Harness: View { | |
| @State private var animating = false | |
| var body: some View { | |
| VStack(spacing: 75) { | |
| Group { | |
| AnimatedHeart (animating: animating) | |
| .fillColor(heartColor) | |
| AnimatedHeart(animating: animating, debug: true) | |
| .strokeColor(.black, lineWidth: 2) | |
| .layoutGuide(gridConfig) | |
| } | |
| .frame(200) | |
| } | |
| .onAppear { | |
| withAnimation(Animation.easeOut(duration: 0.2).repeatForever(autoreverses : true)) { | |
| self.animating = true | |
| } | |
| } | |
| } | |
| } | |
| static var previews: some View { | |
| AnimatedHeartShapeDemo_Harness() | |
| .padding(50) | |
| .previewSizeThatFits() | |
| .previewDevice(.iPhone_8) | |
| .showLayoutGuides(true) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment