Last active
February 12, 2022 20:49
-
-
Save CodeSlicing/0a72895576f346bf955a332c327c3128 to your computer and use it in GitHub Desktop.
Source code for CodeSlicing episode on advanced activity indicators - 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
// | |
// CircularActivityIndicatorAdvancedDemo.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. | |
// | |
// Copyright © 2021 Adam Fordyce. All rights reserved. | |
// | |
import PureSwiftUI | |
struct CircularActivityIndicatorAdvancedDemo: View { | |
@State private var animating = false | |
var body: some View { | |
let numSegments = 8 | |
let duration: Double = 1 | |
let rotationPerSegment: Angle = .degrees(360 / numSegments.asDouble) | |
let delayPerSegment = duration / numSegments.asDouble | |
GeometryReader { (geo: GeometryProxy) in | |
ZStack { | |
Color.clear | |
ForEach(0..<numSegments) { index in | |
let delay = delayPerSegment * index.asDouble | |
Capsule() | |
.fillColor(.black) | |
.frame(geo.sizeScaled(0.15, 0.3)) | |
.yOffset(-geo.heightScaled(0.35)) | |
.rotate(rotationPerSegment * index) | |
.opacityIfNot(animating, 0) | |
.animation(Animation.linear(duration: 0.001).delay(delay)) | |
.modifier(OpacityFadingModifier(fadeFactor: 1.4, animating: animating)) | |
.animation(Animation.linear(duration: duration).repeatForever(autoreverses: false).delay(delay)) | |
} | |
} | |
} | |
.onAppear { | |
animating = true | |
} | |
} | |
} | |
private struct OpacityFadingModifier: AnimatableModifier { | |
private let fadeFactor: Double | |
var animatableData: Double | |
init(fadeFactor: Double = 1, animating: Bool) { | |
self.fadeFactor = fadeFactor | |
animatableData = animating ? 1 : 0 | |
} | |
private var animatedOpacity: Double { | |
(fadeFactor - animatableData) / fadeFactor | |
} | |
func body(content: Content) -> some View { | |
content | |
.opacity(animatedOpacity) | |
.animation(nil) | |
} | |
} | |
struct CircularActivityIndicatorAdvancedDemo_Previews: PreviewProvider { | |
struct CircularActivityIndicatorAdvancedDemo_Harness: View { | |
var body: some View { | |
ZStack { | |
CircularActivityIndicatorAdvancedDemo() | |
.frame(300) | |
} | |
.greedyFrame() | |
.ignoresSafeArea() | |
} | |
} | |
static var previews: some View { | |
CircularActivityIndicatorAdvancedDemo_Harness() | |
.previewDevice(.iPhone_12_Pro_Max) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment