Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CodeSlicing/df981c17634720881f95e1082b24f08a to your computer and use it in GitHub Desktop.
Save CodeSlicing/df981c17634720881f95e1082b24f08a to your computer and use it in GitHub Desktop.
Native source code for CodeSlicing episode on simple linear activity indicators
//
// HorizontalActivityIndicatorDemoNative.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
private extension Color {
static let clearRed = Color.red.opacity(0)
}
private let numSegments = 15
struct HorizontalActivityIndicatorDemoNative: View {
@State private var animating = false
@State private var numComplete = 0
var body: some View {
let numSegments = 15
GeometryReader { (geo: GeometryProxy) in
HStack(spacing: 0) {
ForEach(0..<numSegments) { index in
Capsule()
.frame(width: geo.size.width * 0.02)
if index < numSegments - 1 {
Spacer()
.frame(minWidth: 0)
}
}
}
// .background(
.mask(
HorizontalActivityMask(debug: false))
}
}
}
private struct HorizontalActivityMask: View {
var debug = false
@State private var animating = false
var body: some View {
GeometryReader { (geo: GeometryProxy) in
LinearGradient(gradient: Gradient(stops: [
Gradient.Stop(color: .clearRed, location: 0.1),
Gradient.Stop(color: .red, location: 0.5),
Gradient.Stop(color: .clearRed, location: 0.9),
]), startPoint: .leading, endPoint: .trailing)
.offset(x: debug ? 0 : -geo.size.width)
.offset(x: animating && !debug ? geo.size.width * 2 : 0)
}
.onAppear {
withAnimation(Animation.linear(duration: 1).repeatForever(autoreverses: true)) {
animating = true
}
}
}
}
struct HorizontalActivityIndicatorDemoNative_Previews: PreviewProvider {
struct HorizontalActivityIndicatorDemoNative_Harness: View {
var body: some View {
HorizontalActivityIndicatorDemoNative()
.frame(width: 250, height: 30)
}
}
static var previews: some View {
HorizontalActivityIndicatorDemoNative_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