@@ -31,15 +31,49 @@ class ZLAdjustSlider: UIView {
31
31
32
32
let sliderWidth : CGFloat = 5
33
33
34
- lazy var valueLabel = UILabel ( )
35
-
36
- lazy var separator = UIView ( )
37
-
38
- lazy var shadowView = UIView ( )
39
-
40
- lazy var whiteView = UIView ( )
41
-
42
- lazy var tintView = UIView ( )
34
+ lazy var valueLabel : UILabel = {
35
+ let label = UILabel ( )
36
+ label. font = UIFont . systemFont ( ofSize: 12 )
37
+ label. layer. shadowColor = UIColor . black. withAlphaComponent ( 0.6 ) . cgColor
38
+ label. layer. shadowOffset = . zero
39
+ label. layer. shadowOpacity = 1
40
+ label. textColor = . white
41
+ label. textAlignment = ZLImageEditorUIConfiguration . default ( ) . adjustSliderType == . vertical ? . right : . center
42
+ label. adjustsFontSizeToFitWidth = true
43
+ label. minimumScaleFactor = 0.6
44
+ return label
45
+ } ( )
46
+
47
+ lazy var separator : UIView = {
48
+ let view = UIView ( )
49
+ view. backgroundColor = zlRGB ( 230 , 230 , 230 )
50
+ return view
51
+ } ( )
52
+
53
+ lazy var shadowView : UIView = {
54
+ let view = UIView ( )
55
+ view. backgroundColor = . zl. adjustSliderNormalColor
56
+ view. layer. cornerRadius = sliderWidth / 2
57
+ view. layer. shadowColor = UIColor . black. withAlphaComponent ( 0.4 ) . cgColor
58
+ view. layer. shadowOffset = . zero
59
+ view. layer. shadowOpacity = 1
60
+ view. layer. shadowRadius = 3
61
+ return view
62
+ } ( )
63
+
64
+ lazy var whiteView : UIView = {
65
+ let view = UIView ( )
66
+ view. backgroundColor = . zl. adjustSliderNormalColor
67
+ view. layer. cornerRadius = sliderWidth / 2
68
+ view. layer. masksToBounds = true
69
+ return view
70
+ } ( )
71
+
72
+ lazy var tintView : UIView = {
73
+ let view = UIView ( )
74
+ view. backgroundColor = . zl. adjustSliderTintColor
75
+ return view
76
+ } ( )
43
77
44
78
lazy var pan = UIPanGestureRecognizer ( target: self , action: #selector( panAction ( _: ) ) )
45
79
@@ -52,6 +86,8 @@ class ZLAdjustSlider: UIView {
52
86
53
87
private var valueForPanBegan : Float = 0
54
88
89
+ private var isVertical = ZLImageEditorUIConfiguration . default ( ) . adjustSliderType == . vertical
90
+
55
91
var beginAdjust : ( ( ) -> Void ) ?
56
92
57
93
var valueChanged : ( ( Float ) -> Void ) ?
@@ -77,52 +113,48 @@ class ZLAdjustSlider: UIView {
77
113
override func layoutSubviews( ) {
78
114
super. layoutSubviews ( )
79
115
80
- shadowView. frame = CGRect ( x: 40 , y: 0 , width: sliderWidth, height: bounds. height)
81
- whiteView. frame = CGRect ( x: 40 , y: 0 , width: sliderWidth, height: bounds. height)
82
- tintView. frame = calculateTintFrame ( )
83
- let separatorH : CGFloat = 1
84
- separator. frame = CGRect ( x: 0 , y: ( bounds. height - separatorH) / 2 , width: sliderWidth, height: separatorH)
85
- valueLabel. frame = CGRect ( x: 0 , y: bounds. height / 2 - 10 , width: 38 , height: 20 )
116
+ if isVertical {
117
+ shadowView. frame = CGRect ( x: 40 , y: 0 , width: sliderWidth, height: bounds. height)
118
+ whiteView. frame = shadowView. frame
119
+ tintView. frame = calculateTintFrame ( )
120
+ let separatorH : CGFloat = 1
121
+ separator. frame = CGRect ( x: 0 , y: ( bounds. height - separatorH) / 2 , width: sliderWidth, height: separatorH)
122
+ valueLabel. frame = CGRect ( x: 0 , y: bounds. height / 2 - 10 , width: 38 , height: 20 )
123
+ } else {
124
+ valueLabel. frame = CGRect ( x: 0 , y: 0 , width: zl. width, height: 38 )
125
+ shadowView. frame = CGRect ( x: 0 , y: valueLabel. zl. bottom + 2 , width: zl. width, height: sliderWidth)
126
+ whiteView. frame = shadowView. frame
127
+ tintView. frame = calculateTintFrame ( )
128
+ let separatorW : CGFloat = 1
129
+ separator. frame = CGRect ( x: ( zl. width - separatorW) / 2 , y: 0 , width: separatorW, height: sliderWidth)
130
+ }
86
131
}
87
132
88
133
private func setupUI( ) {
89
- shadowView. backgroundColor = . zl. adjustSliderNormalColor
90
- shadowView. layer. cornerRadius = sliderWidth / 2
91
- shadowView. layer. shadowColor = UIColor . black. withAlphaComponent ( 0.4 ) . cgColor
92
- shadowView. layer. shadowOffset = . zero
93
- shadowView. layer. shadowOpacity = 1
94
- shadowView. layer. shadowRadius = 3
95
134
addSubview ( shadowView)
96
-
97
- whiteView. backgroundColor = . zl. adjustSliderNormalColor
98
- whiteView. layer. cornerRadius = sliderWidth / 2
99
- whiteView. layer. masksToBounds = true
100
135
addSubview ( whiteView)
101
-
102
- tintView. backgroundColor = . zl. adjustSliderTintColor
103
136
whiteView. addSubview ( tintView)
104
-
105
- separator. backgroundColor = zlRGB ( 230 , 230 , 230 )
106
137
whiteView. addSubview ( separator)
107
-
108
- valueLabel. font = UIFont . systemFont ( ofSize: 12 )
109
- valueLabel. layer. shadowColor = UIColor . black. withAlphaComponent ( 0.6 ) . cgColor
110
- valueLabel. layer. shadowOffset = . zero
111
- valueLabel. layer. shadowOpacity = 1
112
- valueLabel. textColor = . white
113
- valueLabel. textAlignment = . right
114
- valueLabel. adjustsFontSizeToFitWidth = true
115
- valueLabel. minimumScaleFactor = 0.6
116
138
addSubview ( valueLabel)
117
139
}
118
140
119
141
private func calculateTintFrame( ) -> CGRect {
120
- let totalH = bounds. height / 2
121
- let tintH = totalH * abs( CGFloat ( value) ) / CGFloat( ZLAdjustSlider . maximumValue)
122
- if value > 0 {
123
- return CGRect ( x: 0 , y: totalH - tintH, width: sliderWidth, height: tintH)
142
+ if isVertical {
143
+ let totalH = zl. height / 2
144
+ let tintH = totalH * abs( CGFloat ( value) ) / CGFloat( ZLAdjustSlider . maximumValue)
145
+ if value > 0 {
146
+ return CGRect ( x: 0 , y: totalH - tintH, width: sliderWidth, height: tintH)
147
+ } else {
148
+ return CGRect ( x: 0 , y: totalH, width: sliderWidth, height: tintH)
149
+ }
124
150
} else {
125
- return CGRect ( x: 0 , y: totalH, width: sliderWidth, height: tintH)
151
+ let totalW = zl. width / 2
152
+ let tintW = totalW * abs( CGFloat ( value) ) / CGFloat( ZLAdjustSlider . maximumValue)
153
+ if value > 0 {
154
+ return CGRect ( x: totalW, y: 0 , width: tintW, height: sliderWidth)
155
+ } else {
156
+ return CGRect ( x: totalW - tintW, y: 0 , width: tintW, height: sliderWidth)
157
+ }
126
158
}
127
159
}
128
160
@@ -133,8 +165,9 @@ class ZLAdjustSlider: UIView {
133
165
valueForPanBegan = value
134
166
beginAdjust ? ( )
135
167
} else if pan. state == . changed {
136
- let y = - translation. y / 100
137
- var temp = valueForPanBegan + Float( y)
168
+ let transValue = isVertical ? - translation. y : translation. x
169
+ let totalLength = isVertical ? zl. height / 2 : zl. width / 2
170
+ var temp = valueForPanBegan + Float( transValue / totalLength)
138
171
temp = max ( ZLAdjustSlider . minimumValue, min ( ZLAdjustSlider . maximumValue, temp) )
139
172
140
173
if ( - 0.0049 ..< 0.005 ) ~= temp {
@@ -152,7 +185,6 @@ class ZLAdjustSlider: UIView {
152
185
let style = ZLImageEditorConfiguration . default ( ) . impactFeedbackStyle. uiFeedback
153
186
UIImpactFeedbackGenerator ( style: style) . impactOccurred ( )
154
187
}
155
-
156
188
} else {
157
189
valueForPanBegan = value
158
190
endAdjust ? ( )
0 commit comments