Skip to content

Instantly share code, notes, and snippets.

@labradon

labradon/ActivityRing.swift Secret

Last active Jul 11, 2020
Embed
What would you like to do?
Final code for "Implementing the Apple Watch activity rings in SwiftUI - Part 1"
struct ActivityRing: View {
var progress: Double
var lineWidth: CGFloat
var gradient: Gradient
var body: some View {
// Main ring
Circle()
.rotation(.degrees(-90))
.trim(from: 0, to: CGFloat(progress))
.stroke(
AngularGradient(gradient: gradient,
center: .center,
startAngle: .degrees(-90),
endAngle: .degrees(progress * 360 - 90)),
style: .init(lineWidth: lineWidth, lineCap: .round)
)
.overlay(
GeometryReader { geometry in
// End round butt and shadow
Circle()
.fill(self.gradient.stops[1].color)
.frame(width: self.lineWidth, height: self.lineWidth)
.position(x: geometry.size.width / 2, y: geometry.size.height / 2)
.offset(x: min(geometry.size.width, geometry.size.height)/2)
.rotationEffect(.degrees(self.progress * 360 - 90))
.shadow(color: .black, radius: self.lineWidth/2, x: 0, y: 0)
}
.clipShape(
// Clip end round line cap and shadow to front
Circle()
.rotation(.degrees(-90 + self.progress * 360 - 0.5))
.trim(from: 0, to: 0.25)
.stroke(style: .init(lineWidth: self.lineWidth))
)
)
.scaledToFit()
.padding(lineWidth/2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment