Skip to content

Instantly share code, notes, and snippets.

@CastIrony
Created June 28, 2019 07:55
Show Gist options
  • Save CastIrony/1dab629b2041992ba685b0872a9044c0 to your computer and use it in GitHub Desktop.
Save CastIrony/1dab629b2041992ba685b0872a9044c0 to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// BreatheUI WatchKit Extension
//
// Created by Joel Bernstein on 6/24/19.
// Copyright © 2019 Joel Bernstein. All rights reserved.
//
import SwiftUI
struct ContentView : View {
@State var minutes = 1.0
var circleCount: Double { minutes + 5 }
var roundedCircleCount: Int { Int(ceil(circleCount)) }
var body: some View {
GeometryReader { proxy in
ZStack {
ForEach(1...self.roundedCircleCount) { index in
Circle()
.rotation(self.calculateAngle(index), anchor: UnitPoint(x: 0.5, y: 1))
.scale(0.57)
.position(CGPoint(x: proxy.size.width * 0.5, y: proxy.size.width * 0.25))
.foregroundColor(Color.white.opacity(self.calculateOpacity(index)))
}
}
}
.focusable(true)
.digitalCrownRotation($minutes, from: 1, through: 5, by: 1, sensitivity: .low, isContinuous: false, isHapticFeedbackEnabled: true)
}
func calculateAngle(_ index: Int) -> Angle
{
return .degrees(-((Double(index) - 0.5) * (360.0 / self.circleCount)))
}
func calculateOpacity(_ index: Int) -> Double
{
let fadeThreshold = floor(circleCount)
return index <= Int(fadeThreshold) ? 0.5 : 0.5 * ( circleCount - fadeThreshold )
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment