-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWatchOS.swift
135 lines (117 loc) · 4.83 KB
/
WatchOS.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
131
132
133
134
135
//You may have to change the Corner Radius and some other propertites for it to fit on devices that arent WatchOS Series 10
//If you can make it dynamic please submit pull request to fix it, thank you.
import SwiftUI
import WatchKit
struct GlowEffect: View {
@State private var gradientStops: [Gradient.Stop] = GlowEffect.generateGradientStops()
var body: some View {
ZStack {
EffectNoBlur(gradientStops: gradientStops, width: 5)
.onAppear {
// Start a timer to update the gradient stops every second
Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { _ in
withAnimation(.easeInOut(duration: 0.5)) {
gradientStops = GlowEffect.generateGradientStops()
}
}
}
Effect(gradientStops: gradientStops, width: 7, blur: 4)
.onAppear {
// Start a timer to update the gradient stops every second
Timer.scheduledTimer(withTimeInterval: 0.3, repeats: true) { _ in
withAnimation(.easeInOut(duration: 0.6)) {
gradientStops = GlowEffect.generateGradientStops()
}
}
}
Effect(gradientStops: gradientStops, width: 9, blur: 12)
.onAppear {
// Start a timer to update the gradient stops every second
Timer.scheduledTimer(withTimeInterval: 0.4, repeats: true) { _ in
withAnimation(.easeInOut(duration: 0.8)) {
gradientStops = GlowEffect.generateGradientStops()
}
}
}
Effect(gradientStops: gradientStops, width: 12, blur: 15)
.onAppear {
// Start a timer to update the gradient stops every second
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { _ in
withAnimation(.easeInOut(duration: 1)) {
gradientStops = GlowEffect.generateGradientStops()
}
}
}
}
}
// Function to generate random gradient stops
static func generateGradientStops() -> [Gradient.Stop] {
[
Gradient.Stop(color: Color(hex: "BC82F3"), location: Double.random(in: 0...1)),
Gradient.Stop(color: Color(hex: "F5B9EA"), location: Double.random(in: 0...1)),
Gradient.Stop(color: Color(hex: "8D9FFF"), location: Double.random(in: 0...1)),
Gradient.Stop(color: Color(hex: "FF6778"), location: Double.random(in: 0...1)),
Gradient.Stop(color: Color(hex: "FFBA71"), location: Double.random(in: 0...1)),
Gradient.Stop(color: Color(hex: "C686FF"), location: Double.random(in: 0...1))
].sorted { $0.location < $1.location }
}
}
struct Effect: View {
var gradientStops: [Gradient.Stop]
var width: Double
var blur: Double
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 50)
.strokeBorder(
AngularGradient(
gradient: Gradient(stops: gradientStops),
center: .center
),
lineWidth: width
)
.frame(
width: WKInterfaceDevice.current().screenBounds.width,
height: WKInterfaceDevice.current().screenBounds.height
)
.padding(.top, -17)
.blur(radius: blur)
}
}
}
struct EffectNoBlur: View {
var gradientStops: [Gradient.Stop]
var width: Double
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 50)
.strokeBorder(
AngularGradient(
gradient: Gradient(stops: gradientStops),
center: .center
),
lineWidth: width
)
.frame(
width: WKInterfaceDevice.current().screenBounds.width,
height: WKInterfaceDevice.current().screenBounds.height
)
.padding(.top, -17)
}
}
}
extension Color {
init(hex: String) {
let scanner = Scanner(string: hex)
_ = scanner.scanString("#")
var hexNumber: UInt64 = 0
scanner.scanHexInt64(&hexNumber)
let r = Double((hexNumber & 0xff0000) >> 16) / 255
let g = Double((hexNumber & 0x00ff00) >> 8) / 255
let b = Double(hexNumber & 0x0000ff) / 255
self.init(red: r, green: g, blue: b)
}
}
#Preview {
GlowEffect()
}