Created
August 28, 2021 06:37
-
-
Save CodeSlicing/ba8aaa9844fb1e287574242c6037af47 to your computer and use it in GitHub Desktop.
Native source code for CodeSlicing episode on simple circular activity indicators
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
// | |
// CircularActivityIndicatorDemoNative.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 SwiftUI | |
struct CircularActivityIndicatorDemoNative: View { | |
var body: some View { | |
GeometryReader { (geo: GeometryProxy) in | |
let numSegments = 12 | |
let degreesPerSegment = 360 / Double(numSegments) | |
ZStack { | |
Color.clear | |
ForEach(0..<numSegments) { index in | |
Capsule() | |
.frame(width: geo.size.width * 0.25, height: geo.size.height * 0.06) | |
.offset(x: geo.size.width * 0.37) | |
.rotationEffect(.degrees(degreesPerSegment * Double(index))) | |
} | |
} | |
// .background( | |
.mask( | |
CircularActivityMask() | |
.scaleEffect(1.2) | |
.blur(radius: geo.size.width * 0.05) | |
) | |
} | |
} | |
} | |
private extension Color { | |
static let clearRed = Color.red.opacity(0) | |
} | |
private struct CircularActivityMask: View { | |
@State private var animating = false | |
var body: some View { | |
AngularGradient(gradient: Gradient(stops: [ | |
Gradient.Stop(color: .clearRed, location: 0.4), | |
Gradient.Stop(color: .red, location: 1) | |
]), center: .center, angle: animating ? .degrees(270) : .degrees(-90)) | |
.onAppear { | |
withAnimation(Animation.linear(duration: 1).repeatForever(autoreverses: false)) { | |
animating = true | |
} | |
} | |
} | |
} | |
struct CircularActivityIndicatorDemoNative_Previews: PreviewProvider { | |
struct CircularActivityIndicatorDemoNative_Harness: View { | |
var body: some View { | |
CircularActivityIndicatorDemoNative() | |
.frame(width: 300, height: 300) | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.ignoresSafeArea() | |
} | |
} | |
static var previews: some View { | |
CircularActivityIndicatorDemoNative_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