Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
struct ProgressIndicator: NSViewRepresentable {
@Binding var style: NSProgressIndicator.Style
func makeNSView(context: NSViewRepresentableContext<ProgressIndicator>) -> NSProgressIndicator {
let result = NSProgressIndicator()
result.isIndeterminate = true
result.startAnimation(nil)
return result
}
func updateNSView(_ nsView: NSProgressIndicator, context: NSViewRepresentableContext<ProgressIndicator>) {
nsView.style = style
}
}
//
// ContentView.swift
// ActivityIndicator
//
// Created by Chris Eidhof on 11.06.19.
// Copyright © 2019 Chris Eidhof. All rights reserved.
//
import SwiftUI
struct RelativePosition: ViewModifier {
let x, y: CGFloat
init(x: CGFloat, y: CGFloat) {
self.x = x
self.y = y
}
func body(content: Content) -> some View {
GeometryReader { proxy in
content.position(x: self.x * proxy.size.width, y: self.y * proxy.size.height)
}
}
}
extension View {
func relativePosition(x: CGFloat, y: CGFloat) -> some View {
return Modified(content: self, modifier: RelativePosition(x: x, y: y))
}
}
struct ActivityIndicator: View {
let numberOfSegments: Int
init(numberOfSegments: Int = 10) {
self.numberOfSegments = numberOfSegments
}
let relativeHeight: CGFloat = 0.25
var y: CGFloat {
return relativeHeight - (CGFloat(numberOfSegments)/80)
}
let from: Color = .black
var body: some View {
ZStack {
ForEach(0..<self.numberOfSegments) { num in
Capsule()
.fill(self.from)
.brightness(Double(num)/Double(self.numberOfSegments))
.relativeSize(width: 0.1, height: self.relativeHeight)
.relativePosition(x: 0.5, y: self.y)
.rotationEffect(.degrees(Double(num) * (360/Double(self.numberOfSegments))), anchor: .center)
}
}.drawingGroup()
}
}
struct ContentView : View {
var body: some View {
ActivityIndicator()
.frame(width: 50, height: 50)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
Group {
ForEach(1..<10) {
ActivityIndicator(numberOfSegments: $0 * 3)
.frame(width: 50, height: 50)
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.