Skip to content

Instantly share code, notes, and snippets.

@labradon
Last active July 11, 2020 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save labradon/d537eeb307aaef13ca505ee2743533d1 to your computer and use it in GitHub Desktop.
Save labradon/d537eeb307aaef13ca505ee2743533d1 to your computer and use it in GitHub Desktop.
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