-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInnerShadowModifier.swift
More file actions
139 lines (123 loc) · 4.72 KB
/
InnerShadowModifier.swift
File metadata and controls
139 lines (123 loc) · 4.72 KB
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
136
137
138
139
//
// InnerShadowModifier.swift
// SSNeumorphicView
//
// Created by Simform Solutions on 30/07/24.
//
import SwiftUI
/// Custom modifier to add inner shadow to a View.
public struct InnerShadowModifier<S: Shape>: ViewModifier {
// MARK: - Variables
/// The shape to which the inner shadow will be applied.
var shape: S
/// The color of the dark shadow.
var shadowColorDark: Color
/// The color of the light shadow.
var shadowColorLight: Color
/// The width of the shadow.
var shadowWidth: CGFloat
/// The blur radius of the shadow.
var radius: CGFloat
/// The offset of the shadow.
var offset: CGFloat
// MARK: - Initializers
/// Creates an `InnerShadowModifier` with specified parameters.
/// - Parameters:
/// - shape: The shape to which the inner shadow will be applied.
/// - shadowColorDark: The color of the dark shadow (default is `Color.Neumorphic.darkShadow`).
/// - shadowColorLight: The color of the light shadow (default is `Color.Neumorphic.lightShadow`).
/// - radius: The blur radius of the shadow.
/// - shadowWidth: The width of the shadow.
/// - offset: The offset of the shadow.
public init(shape: S,
shadowColorDark: Color = Color.Neumorphic.darkShadow,
shadowColorLight: Color = Color.Neumorphic.lightShadow,
radius: CGFloat,
shadowWidth: CGFloat,
offset: CGFloat) {
self.shadowColorDark = shadowColorDark
self.shadowColorLight = shadowColorLight
self.radius = radius
self.shape = shape
self.offset = offset
self.shadowWidth = shadowWidth
}
// MARK: - Functions
/// Adds an overlay with the specified shadow properties.
/// - Parameters:
/// - shape: The shape to which the overlay will be applied.
/// - linearGradient: The gradient used for the shadow.
/// - strokeColor: The color of the stroke.
/// - offset: The offset of the shadow.
/// - shadowWidth: The width of the shadow.
/// - radius: The blur radius of the shadow.
/// - Returns: A `View` with the applied overlay.
fileprivate func addOverlay(
shape: S,
linearGradient: LinearGradient,
strokeColor: Color,
offset: CGFloat ,
shadowWidth: CGFloat,
radius: CGFloat
) -> some View {
return shape
.stroke(strokeColor, lineWidth: shadowWidth)
.blur(radius: radius)
.offset(x: offset, y: offset)
.mask(shape.fill(linearGradient))
}
// MARK: - Modifier Body
/// Applies the inner shadow effect to the content view.
/// - Parameter content: The view to which the modifier is applied.
/// - Returns: The view with the inner shadow effect.
public func body(content: Content) -> some View {
if #available(iOS 15.0, *) {
content
.overlay {
addOverlay(
shape: shape,
linearGradient: LinearGradient(shadowColorDark, Color.clear),
strokeColor: shadowColorDark,
offset: offset,
shadowWidth: shadowWidth,
radius: radius
)
}
.overlay(
addOverlay(
shape: shape,
linearGradient: LinearGradient(Color.clear, shadowColorLight),
strokeColor: shadowColorLight,
offset: -offset,
shadowWidth: shadowWidth,
radius: radius
)
)
} else {
// Fallback on earlier versions
content
.overlay(
addOverlay(
shape: shape,
linearGradient: LinearGradient(shadowColorDark, Color.clear),
strokeColor: shadowColorDark,
offset: offset,
shadowWidth: shadowWidth,
radius: radius
),
alignment: .center
)
.overlay(
addOverlay(
shape: shape,
linearGradient: LinearGradient(Color.clear, shadowColorLight),
strokeColor: shadowColorLight,
offset: -offset,
shadowWidth: shadowWidth,
radius: radius
),
alignment: .center
)
}
}
}