generated from CypherPoet/swift-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAnimatableGradientModifier.swift
60 lines (40 loc) · 1.3 KB
/
AnimatableGradientModifier.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
#if os(iOS) || os(tvOS)
import SwiftUI
protocol AnimatableGradientModifier: AnimatableModifier {
associatedtype BaseShape: Shape
associatedtype GradientShapeStyle: ShapeStyle
var baseShape: BaseShape { get }
var startColors: [UIColor] { get }
var endColors: [UIColor] { get }
var completionPercentage: CGFloat { get set }
func gradientFill(in geometry: GeometryProxy) -> GradientShapeStyle
}
// MARK: - Animatable Data
extension AnimatableGradientModifier {
var animatableData: CGFloat {
get { completionPercentage }
set { completionPercentage = newValue }
}
}
// MARK - Default Body
extension AnimatableGradientModifier {
func body(content: Content) -> some View {
GeometryReader { geometry in
self.baseShape
.fill(self.gradientFill(in: geometry))
.frame(width: geometry.size.width, height: geometry.size.height)
}
}
}
// MARK: - Computeds
extension AnimatableGradientModifier {
var gradient: Gradient {
Gradient(colors: self.gradientColors)
}
var gradientColors: [Color] {
zip(startColors, endColors).map { (startColor, endColor) in
startColor.interpolate(between: endColor, by: completionPercentage)
}
}
}
#endif