Skip to content

Commit d2ed114

Browse files
committed
Add horizontal adjust slider
1 parent 331422d commit d2ed114

File tree

4 files changed

+119
-54
lines changed

4 files changed

+119
-54
lines changed

Sources/General/ZLAdjustSlider.swift

Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,49 @@ class ZLAdjustSlider: UIView {
3131

3232
let sliderWidth: CGFloat = 5
3333

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+
}()
4377

4478
lazy var pan = UIPanGestureRecognizer(target: self, action: #selector(panAction(_:)))
4579

@@ -52,6 +86,8 @@ class ZLAdjustSlider: UIView {
5286

5387
private var valueForPanBegan: Float = 0
5488

89+
private var isVertical = ZLImageEditorUIConfiguration.default().adjustSliderType == .vertical
90+
5591
var beginAdjust: (() -> Void)?
5692

5793
var valueChanged: ((Float) -> Void)?
@@ -77,52 +113,48 @@ class ZLAdjustSlider: UIView {
77113
override func layoutSubviews() {
78114
super.layoutSubviews()
79115

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+
}
86131
}
87132

88133
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
95134
addSubview(shadowView)
96-
97-
whiteView.backgroundColor = .zl.adjustSliderNormalColor
98-
whiteView.layer.cornerRadius = sliderWidth / 2
99-
whiteView.layer.masksToBounds = true
100135
addSubview(whiteView)
101-
102-
tintView.backgroundColor = .zl.adjustSliderTintColor
103136
whiteView.addSubview(tintView)
104-
105-
separator.backgroundColor = zlRGB(230, 230, 230)
106137
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
116138
addSubview(valueLabel)
117139
}
118140

119141
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+
}
124150
} 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+
}
126158
}
127159
}
128160

@@ -133,8 +165,9 @@ class ZLAdjustSlider: UIView {
133165
valueForPanBegan = value
134166
beginAdjust?()
135167
} 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)
138171
temp = max(ZLAdjustSlider.minimumValue, min(ZLAdjustSlider.maximumValue, temp))
139172

140173
if (-0.0049..<0.005) ~= temp {
@@ -152,7 +185,6 @@ class ZLAdjustSlider: UIView {
152185
let style = ZLImageEditorConfiguration.default().impactFeedbackStyle.uiFeedback
153186
UIImpactFeedbackGenerator(style: style).impactOccurred()
154187
}
155-
156188
} else {
157189
valueForPanBegan = value
158190
endAdjust?()

Sources/General/ZLEditImageViewController.swift

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ open class ZLEditImageViewController: UIViewController {
302302

303303
let canRedo = ZLImageEditorConfiguration.default().canRedo
304304

305+
var hasAdjustedImage = false
306+
305307
@objc public var editFinishBlock: ((UIImage, ZLEditImageModel?) -> Void)?
306308

307309
override open var prefersStatusBarHidden: Bool {
@@ -413,6 +415,7 @@ open class ZLEditImageViewController: UIViewController {
413415
guard shouldLayout else {
414416
return
415417
}
418+
416419
shouldLayout = false
417420
zl_debugPrint("edit image layout subviews")
418421
var insets = UIEdgeInsets.zero
@@ -439,7 +442,18 @@ open class ZLEditImageViewController: UIViewController {
439442
drawColorCollectionView?.frame = CGRect(x: 20, y: 20, width: revokeBtn.zl.left - 20 - 10, height: drawColViewH)
440443

441444
adjustCollectionView?.frame = CGRect(x: 20, y: 10, width: view.zl.width - 40, height: adjustColViewH)
442-
adjustSlider?.frame = CGRect(x: view.zl.width - 60, y: view.zl.height / 2 - 100, width: 60, height: 200)
445+
if ZLImageEditorUIConfiguration.default().adjustSliderType == .vertical {
446+
adjustSlider?.frame = CGRect(x: view.zl.width - 60, y: view.zl.height / 2 - 100, width: 60, height: 200)
447+
} else {
448+
let sliderHeight: CGFloat = 60
449+
let sliderWidth = UIDevice.current.userInterfaceIdiom == .phone ? view.zl.width - 100 : view.zl.width / 2
450+
adjustSlider?.frame = CGRect(
451+
x: (view.zl.width - sliderWidth) / 2,
452+
y: bottomShadowView.zl.top - sliderHeight,
453+
width: sliderWidth,
454+
height: sliderHeight
455+
)
456+
}
443457

444458
filterCollectionView?.frame = CGRect(x: 20, y: 0, width: view.zl.width - 40, height: filterColViewH)
445459

@@ -645,7 +659,7 @@ open class ZLEditImageViewController: UIViewController {
645659
self?.adjustValueChanged(value)
646660
}
647661
adjustSlider?.endAdjust = { [weak self] in
648-
self?.endAdjust()
662+
self?.hasAdjustedImage = true
649663
}
650664
adjustSlider?.isHidden = true
651665
view.addSubview(adjustSlider!)
@@ -831,6 +845,8 @@ open class ZLEditImageViewController: UIViewController {
831845
selectedTool = nil
832846
}
833847

848+
endAdjust()
849+
834850
drawColorCollectionView?.isHidden = true
835851
filterCollectionView?.isHidden = true
836852
adjustCollectionView?.isHidden = true
@@ -1100,12 +1116,15 @@ open class ZLEditImageViewController: UIViewController {
11001116
}
11011117

11021118
func endAdjust() {
1103-
if tools.contains(.mosaic) {
1104-
generateNewMosaicImageLayer()
1105-
1106-
if !mosaicPaths.isEmpty {
1107-
generateNewMosaicImage()
1108-
}
1119+
defer {
1120+
hasAdjustedImage = false
1121+
}
1122+
1123+
guard tools.contains(.mosaic), hasAdjustedImage else { return }
1124+
generateNewMosaicImageLayer()
1125+
1126+
if !mosaicPaths.isEmpty {
1127+
generateNewMosaicImage()
11091128
}
11101129
}
11111130

Sources/General/ZLImageEditorUIConfiguration+Chaining.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public extension ZLImageEditorUIConfiguration {
3333
return self
3434
}
3535

36+
@discardableResult
37+
func adjustSliderType(_ type: ZLAdjustSliderType) -> ZLImageEditorUIConfiguration {
38+
adjustSliderType = type
39+
return self
40+
}
41+
3642
@discardableResult
3743
func languageType(_ type: ZLImageEditorLanguageType) -> ZLImageEditorUIConfiguration {
3844
languageType = type

Sources/General/ZLImageEditorUIConfiguration.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class ZLImageEditorUIConfiguration: NSObject {
4040
/// HUD style. Defaults to dark.
4141
@objc public var hudStyle: ZLProgressHUD.HUDStyle = .dark
4242

43+
/// Adjust Slider Type
44+
@objc public var adjustSliderType: ZLAdjustSliderType = .vertical
45+
4346
// MARK: Language properties
4447

4548
/// Language for framework.
@@ -135,3 +138,8 @@ enum ZLCustomImageDeploy {
135138

136139
static var imageForKey: [String: UIImage] = [:]
137140
}
141+
142+
@objc public enum ZLAdjustSliderType: Int {
143+
case vertical
144+
case horizontal
145+
}

0 commit comments

Comments
 (0)