-
Notifications
You must be signed in to change notification settings - Fork 16
/
ColorClockView.swift
130 lines (109 loc) · 3.94 KB
/
ColorClockView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// ContentView.swift
// ColorClock
//
// Created by Ganesh Raju Galla on 19/02/23.
//
import SwiftUI
struct ColorClockView: View {
//MARK: - Properties
@State private var hoursAngle:Double = 0
@State private var minutesAngle:Double = 0
let timer = Timer.publish(every: 1.0, on: .main, in: .default).autoconnect()
private let fullCircleValue: Double = 0.2
//MARK: - Body
var body: some View {
ZStack{
LinearGradient(colors: [.AppGray,.AppBlack], startPoint: .topLeading, endPoint: .bottom)
bodyView
}
.ignoresSafeArea()
.onReceive(timer) { time in
withAnimation(.linear(duration: 1)) {
minutesAngle += 360 / (fullCircleValue*60)
hoursAngle += 360 / (fullCircleValue*60*12)
}
}
}
}
//MARK: - Extension
extension ColorClockView{
//MARK: - Body View
var bodyView: some View{
VStack{
ClockView
}
.padding(.vertical,100)
}
//MARK: - ClockView
private var ClockView:some View{
ZStack{
Circle()
.stroke(lineWidth: 20)
.fill(LinearGradient(colors: [.AppGray,.AppBlack], startPoint: .top, endPoint: .bottom))
.blur(radius: 16)
.shadow(color: .AppBlack, radius: 23,x: 0,y: 25)
.shadow(color: .AppGray, radius: 23,x: 0,y: -25)
.frame(width: 330,height: 330,alignment: .center)
Circle()
.fill(LinearGradient(colors: [.AppGray,.AppBlack].reversed(), startPoint: .top, endPoint: .bottom))
.blur(radius: 3)
.shadow(color: .AppGray, radius: 3,x: 0,y: -3)
.shadow(color: .AppBlack, radius: 3,x: 0,y: 3)
.frame(width: 13,height: 13,alignment: .center)
HandsView
.shadow(color: .AppBlack, radius: 10,x: 7,y: 10)
}
}
//MARK: HandsView
private var HandsView: some View{
Canvas{ context ,size in
context.addFilter(.alphaThreshold(min: 0.5,color: .AppGreen))
context.addFilter(.blur(radius: 2))
context.drawLayer { ctx in
let hour = ctx.resolveSymbol(id: 1)!
let minute = ctx.resolveSymbol(id: 2)!
ctx.draw(hour, at: CGPoint(x: size.width/2, y: size.height/2))
ctx.draw(minute, at: CGPoint(x: size.width/2, y: size.height/2))
}
}symbols: {
HoursHandView
.rotationEffect(.degrees(-90))
.tag(1)
MinutesHandView
.rotationEffect(.degrees(-90))
.tag(2)
}
}
//MARK: - HoursHandView
private var HoursHandView: some View{
Capsule(style: .continuous)
.fill(LinearGradient(colors: [.AppGreen,.AppDark], startPoint: .leading, endPoint: .trailing))
.frame(width: 60,height: 13,alignment: .center)
.offset(x: 50)
.rotationEffect(.degrees(hoursAngle))
.shadow(color: .AppBlack, radius: 10,x: -7,y: 5)
}
//MARK: - MinutesHandView
private var MinutesHandView: some View{
Capsule(style: .continuous)
.fill(LinearGradient(colors: [.AppGreen,.AppDark], startPoint: .leading, endPoint: .trailing))
.frame(width: 70,height: 7,alignment: .center)
.offset(x: 55)
.rotationEffect(.degrees(minutesAngle))
.shadow(color: .AppBlack, radius: 10,x: -7,y: 5)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ColorClockView()
}
}
import Foundation
import SwiftUI
extension Color{
static let AppBlack = Color("AppBlack")
static let AppGray = Color("AppGray")
static let AppDark = Color("AppDark")
static let AppGreen = Color("AppGreen")
}