Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Created February 17, 2020 18:54
Show Gist options
  • Save CodeSlicing/adf9bdf6539fc00b8043b6613030a821 to your computer and use it in GitHub Desktop.
Save CodeSlicing/adf9bdf6539fc00b8043b6613030a821 to your computer and use it in GitHub Desktop.
Demonstrates animating a layout guide and points within the layout guide
//
// UpArrowToTickAnimatedIconDemo.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 17/02/2020.
// Copyright © 2020 Adam Fordyce. All rights reserved.
//
import SwiftUI
import PureSwiftUI
private let layoutConfig = LayoutGuideConfig.polar(rings: 8, segments: 24)
struct UpArrowToTickAnimatedIconDemo: View {
@State private var animating = true
var body: some View {
ZStack {
UpToTickAnimatedShape(factor: animating ? 1 : 0)
.stroke(style: .init(lineWidth: 10, lineCap: .round, lineJoin: .round))
.layoutGuide(layoutConfig)
}
.frame(200)
.onAppear {
withAnimation(Animation.easeInOut(duration: 0.5).delay(1).repeatForever()) {
self.animating.toggle()
}
}
}
}
private struct UpToTickAnimatedShape: Shape {
var factor: CGFloat
var animatableData: CGFloat {
get {
factor
}
set {
factor = newValue
}
}
func path(in rect: CGRect) -> Path {
var path = Path()
var polar = layoutConfig.layout(in: rect)
.rotated(165.degrees, anchor: .center, factor: factor)
let p1 = polar[4, 17].to(polar[4, 17], factor)
let p2 = polar[4, 13].to(polar[5, 13], factor)
let p3 = polar[5, 3].to(polar[4, 9], factor)
let p4 = polar[4, 13].to(polar[5, 1], factor)
path.ellipse(rect)
path.move(p1)
path.line(p2)
path.line(p3)
path.line(from: p4, to: p2)
return path
}
}
struct UpToTickAnimatedIconDemo_Previews: PreviewProvider {
struct UpToTickAnimatedIconDemo_Harness: View {
var body: some View {
UpArrowToTickAnimatedIconDemo()
}
}
static var previews: some View {
UpToTickAnimatedIconDemo_Harness()
.showLayoutGuides(true)
.padding(100)
.previewSizeThatFits()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment