Skip to content

Commit ab6b621

Browse files
committed
add initialColor
1 parent 01a6fb6 commit ab6b621

File tree

5 files changed

+72
-96
lines changed

5 files changed

+72
-96
lines changed

Demo/RangeSeekSliderDemo/Base.lproj/Main.storyboard

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@
6767
<userDefinedRuntimeAttribute type="number" keyPath="maxDistance">
6868
<real key="value" value="80"/>
6969
</userDefinedRuntimeAttribute>
70+
<userDefinedRuntimeAttribute type="number" keyPath="minValue">
71+
<real key="value" value="50"/>
72+
</userDefinedRuntimeAttribute>
73+
<userDefinedRuntimeAttribute type="number" keyPath="maxValue">
74+
<real key="value" value="150"/>
75+
</userDefinedRuntimeAttribute>
76+
<userDefinedRuntimeAttribute type="number" keyPath="selectedMinValue">
77+
<real key="value" value="60"/>
78+
</userDefinedRuntimeAttribute>
79+
<userDefinedRuntimeAttribute type="number" keyPath="selectedMaxValue">
80+
<real key="value" value="140"/>
81+
</userDefinedRuntimeAttribute>
7082
</userDefinedRuntimeAttributes>
7183
</view>
7284
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Custom Range and Image Handle:" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="AzH-aT-IDj">
@@ -88,11 +100,14 @@
88100
<real key="value" value="10"/>
89101
</userDefinedRuntimeAttribute>
90102
<userDefinedRuntimeAttribute type="number" keyPath="selectedMinValue">
91-
<real key="value" value="10"/>
103+
<real key="value" value="20"/>
92104
</userDefinedRuntimeAttribute>
93105
<userDefinedRuntimeAttribute type="color" keyPath="colorBetweenHandles">
94106
<color key="value" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
95107
</userDefinedRuntimeAttribute>
108+
<userDefinedRuntimeAttribute type="number" keyPath="selectedMaxValue">
109+
<real key="value" value="80"/>
110+
</userDefinedRuntimeAttribute>
96111
</userDefinedRuntimeAttributes>
97112
</view>
98113
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Range with Step:" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="4K8-GF-10z">
@@ -161,7 +176,7 @@
161176
</label>
162177
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gdz-ut-dL8" customClass="RangeSeekSlider" customModule="RangeSeekSlider">
163178
<rect key="frame" x="16" y="631" width="343" height="65"/>
164-
<color key="tintColor" red="0.86666666670000003" green="0.86666666670000003" blue="0.86666666670000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
179+
<color key="tintColor" red="0.40000000000000002" green="0.40000000000000002" blue="0.40000000000000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
165180
<constraints>
166181
<constraint firstAttribute="height" constant="65" id="wa9-1w-B3f"/>
167182
</constraints>

Demo/RangeSeekSliderDemo/ViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ final class ViewController: UIViewController {
8080
rangeSliderCustomString.tintColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
8181
rangeSliderCustomString.numberFormatter.locale = Locale.current
8282
rangeSliderCustomString.numberFormatter.numberStyle = .currency
83+
rangeSliderCustomString.initialColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
8384
}
8485

8586
fileprivate func priceString(value: CGFloat, numberFormatter: NumberFormatter) -> String {

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ The colorBetweenHandles property sets the color of the line between the two hand
9191
#### `handleBorderColor`
9292
If set it will update the color of the handle borders. Default is `tintColor`.
9393

94+
### `initialColor`
95+
The color of the entire slider when the handle is set to the minimum value and the maximum value. Default is nil.
96+
9497
#### `disableRange`
9598
If true, the control will mimic a normal slider and have only one handle rather than a range.
9699

RangeSeekSlider.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'RangeSeekSlider'
3-
s.version = '1.2.0'
3+
s.version = '1.3.0'
44
s.summary = 'RangeSeedSlider provides a customizable range slider like a UISlider.'
55
s.description = <<-DESC
66
RangeSeedSlider provides a customizable range slider like a UISlider.

Sources/RangeSeekSlider.swift

Lines changed: 50 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,14 @@ import UIKit
3434
public weak var delegate: RangeSeekSliderDelegate?
3535

3636
/// The minimum possible value to select in the range
37-
@IBInspectable public var minValue: CGFloat = 0.0 {
38-
didSet {
39-
refresh()
40-
}
41-
}
37+
@IBInspectable public var minValue: CGFloat = 0.0
4238

4339
/// The maximum possible value to select in the range
44-
@IBInspectable public var maxValue: CGFloat = 100.0 {
45-
didSet {
46-
refresh()
47-
}
48-
}
40+
@IBInspectable public var maxValue: CGFloat = 100.0
4941

5042
/// The preselected minumum value
5143
/// (note: This should be less than the selectedMaxValue)
52-
@IBInspectable public var selectedMinValue: CGFloat = 10.0 {
44+
@IBInspectable public var selectedMinValue: CGFloat = 0.0 {
5345
didSet {
5446
if selectedMinValue < minValue {
5547
selectedMinValue = minValue
@@ -59,7 +51,7 @@ import UIKit
5951

6052
/// The preselected maximum value
6153
/// (note: This should be greater than the selectedMinValue)
62-
@IBInspectable public var selectedMaxValue: CGFloat = 90.0 {
54+
@IBInspectable public var selectedMaxValue: CGFloat = 100.0 {
6355
didSet {
6456
if selectedMaxValue > maxValue {
6557
selectedMaxValue = maxValue
@@ -96,7 +88,6 @@ import UIKit
9688
@IBInspectable public var hideLabels: Bool = false
9789

9890
/// The minimum distance the two selected slider values must be apart. Default is 0.
99-
10091
@IBInspectable public var minDistance: CGFloat = 0.0 {
10192
didSet {
10293
if minDistance < 0.0 {
@@ -106,7 +97,6 @@ import UIKit
10697
}
10798

10899
/// The maximum distance the two selected slider values must be apart. Default is CGFloat.greatestFiniteMagnitude.
109-
110100
@IBInspectable public var maxDistance: CGFloat = .greatestFiniteMagnitude {
111101
didSet {
112102
if maxDistance < 0.0 {
@@ -116,41 +106,22 @@ import UIKit
116106
}
117107

118108
/// The color of the minimum value text label. If not set, the default is the tintColor.
119-
@IBInspectable public var minLabelColor: UIColor? {
120-
didSet {
121-
minLabel.foregroundColor = minLabelColor?.cgColor
122-
}
123-
}
109+
@IBInspectable public var minLabelColor: UIColor?
124110

125111
/// The color of the maximum value text label. If not set, the default is the tintColor.
126-
@IBInspectable public var maxLabelColor: UIColor? {
127-
didSet {
128-
maxLabel.foregroundColor = maxLabelColor?.cgColor
129-
}
130-
}
112+
@IBInspectable public var maxLabelColor: UIColor?
131113

132114
/// Handle slider with custom color, you can set custom color for your handle
133-
@IBInspectable public var handleColor: UIColor? {
134-
didSet {
135-
leftHandle.backgroundColor = handleColor?.cgColor
136-
rightHandle.backgroundColor = handleColor?.cgColor
137-
}
138-
}
115+
@IBInspectable public var handleColor: UIColor?
139116

140117
/// Handle slider with custom border color, you can set custom border color for your handle
141-
@IBInspectable public var handleBorderColor: UIColor? {
142-
didSet {
143-
leftHandle.borderColor = handleBorderColor?.cgColor
144-
rightHandle.borderColor = handleBorderColor?.cgColor
145-
}
146-
}
118+
@IBInspectable public var handleBorderColor: UIColor?
147119

148120
/// Set slider line tint color between handles
149-
@IBInspectable public var colorBetweenHandles: UIColor? {
150-
didSet {
151-
sliderLineBetweenHandles.backgroundColor = colorBetweenHandles?.cgColor
152-
}
153-
}
121+
@IBInspectable public var colorBetweenHandles: UIColor?
122+
123+
/// The color of the entire slider when the handle is set to the minimum value and the maximum value. Default is nil.
124+
@IBInspectable public var initialColor: UIColor?
154125

155126
/// If true, the control will mimic a normal slider and have only one handle rather than a range.
156127
/// In this case, the selectedMinValue will be not functional anymore. Use selectedMaxValue instead to determine the value the user has selected.
@@ -171,14 +142,12 @@ import UIKit
171142
/// Handle slider with custom image, you can set custom image for your handle
172143
@IBInspectable public var handleImage: UIImage? {
173144
didSet {
174-
let startFrame: CGRect = CGRect(x: 0.0, y: 0.0, width: 32.0, height: 32.0)
175-
leftHandle.frame = startFrame
145+
let handleFrame: CGRect = CGRect(x: 0.0, y: 0.0, width: 32.0, height: 32.0)
146+
leftHandle.frame = handleFrame
176147
leftHandle.contents = handleImage?.cgImage
177-
leftHandle.backgroundColor = UIColor.clear.cgColor
178148

179-
rightHandle.frame = startFrame
149+
rightHandle.frame = handleFrame
180150
rightHandle.contents = handleImage?.cgImage
181-
rightHandle.backgroundColor = UIColor.clear.cgColor
182151
}
183152
}
184153

@@ -285,6 +254,7 @@ import UIKit
285254
if handleTracking == .none {
286255
updateLineHeight()
287256
updateLabelValues()
257+
updateColors()
288258
updateHandlePositions()
289259
updateLabelPositions()
290260
}
@@ -294,37 +264,6 @@ import UIKit
294264
return CGSize(width: UIViewNoIntrinsicMetric, height: 65.0)
295265
}
296266

297-
open override var tintColor: UIColor! {
298-
didSet {
299-
guard let color: CGColor = tintColor?.cgColor else { return }
300-
301-
CATransaction.begin()
302-
CATransaction.setAnimationDuration(0.5)
303-
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))
304-
305-
sliderLine.backgroundColor = color
306-
307-
if handleColor == nil {
308-
leftHandle.backgroundColor = color
309-
rightHandle.backgroundColor = color
310-
}
311-
312-
if minLabelColor == nil {
313-
minLabel.foregroundColor = color
314-
}
315-
316-
if maxLabelColor == nil {
317-
maxLabel.foregroundColor = color
318-
}
319-
320-
if colorBetweenHandles == nil {
321-
sliderLineBetweenHandles.backgroundColor = color
322-
}
323-
324-
CATransaction.commit()
325-
}
326-
}
327-
328267

329268
// MARK: - UIControl
330269

@@ -419,25 +358,19 @@ import UIKit
419358
accessibleElements = [leftHandleAccessibilityElement, rightHandleAccessibilityElement]
420359

421360
// draw the slider line
422-
sliderLine.backgroundColor = tintColor.cgColor
423361
layer.addSublayer(sliderLine)
424362

425363
// draw the track distline
426-
sliderLineBetweenHandles.backgroundColor = tintColor.cgColor
427364
layer.addSublayer(sliderLineBetweenHandles)
428365

429366
// draw the minimum slider handle
430367
leftHandle.cornerRadius = handleDiameter / 2.0
431-
leftHandle.backgroundColor = tintColor.cgColor
432368
leftHandle.borderWidth = handleBorderWidth
433-
leftHandle.borderColor = handleBorderColor?.cgColor
434369
layer.addSublayer(leftHandle)
435370

436371
// draw the maximum slider handle
437372
rightHandle.cornerRadius = handleDiameter / 2.0
438-
rightHandle.backgroundColor = tintColor.cgColor
439373
rightHandle.borderWidth = handleBorderWidth
440-
rightHandle.borderColor = handleBorderColor?.cgColor
441374
layer.addSublayer(rightHandle)
442375

443376
let handleFrame: CGRect = CGRect(x: 0.0, y: 0.0, width: handleDiameter, height: handleDiameter)
@@ -452,22 +385,12 @@ import UIKit
452385
minLabel.alignmentMode = kCAAlignmentCenter
453386
minLabel.frame = labelFrame
454387
minLabel.contentsScale = UIScreen.main.scale
455-
if let cgColor = minLabelColor?.cgColor {
456-
minLabel.foregroundColor = cgColor
457-
} else {
458-
minLabel.foregroundColor = tintColor.cgColor
459-
}
460388
layer.addSublayer(minLabel)
461389

462390
maxLabelFont = UIFont.systemFont(ofSize: labelFontSize)
463391
maxLabel.alignmentMode = kCAAlignmentCenter
464392
maxLabel.frame = labelFrame
465393
maxLabel.contentsScale = UIScreen.main.scale
466-
if let cgColor = maxLabelColor?.cgColor {
467-
maxLabel.foregroundColor = cgColor
468-
} else {
469-
maxLabel.foregroundColor = tintColor.cgColor
470-
}
471394
layer.addSublayer(maxLabel)
472395

473396
refresh()
@@ -541,6 +464,39 @@ import UIKit
541464
}
542465
}
543466

467+
private func updateColors() {
468+
let isInitial: Bool = selectedMinValue == minValue && selectedMaxValue == maxValue
469+
if let initialColor = initialColor?.cgColor, isInitial {
470+
minLabel.foregroundColor = initialColor
471+
maxLabel.foregroundColor = initialColor
472+
sliderLineBetweenHandles.backgroundColor = initialColor
473+
sliderLine.backgroundColor = initialColor
474+
475+
let color: CGColor = (handleImage == nil) ? initialColor : UIColor.clear.cgColor
476+
leftHandle.backgroundColor = color
477+
leftHandle.borderColor = color
478+
rightHandle.backgroundColor = color
479+
rightHandle.borderColor = color
480+
} else {
481+
let tintCGColor: CGColor = tintColor.cgColor
482+
minLabel.foregroundColor = minLabelColor?.cgColor ?? tintCGColor
483+
maxLabel.foregroundColor = maxLabelColor?.cgColor ?? tintCGColor
484+
sliderLineBetweenHandles.backgroundColor = colorBetweenHandles?.cgColor ?? tintCGColor
485+
sliderLine.backgroundColor = tintCGColor
486+
487+
let color: CGColor
488+
if let _ = handleImage {
489+
color = UIColor.clear.cgColor
490+
} else {
491+
color = handleColor?.cgColor ?? tintCGColor
492+
}
493+
leftHandle.backgroundColor = color
494+
leftHandle.borderColor = color
495+
rightHandle.backgroundColor = color
496+
rightHandle.borderColor = color
497+
}
498+
}
499+
544500
private func updateAccessibilityElements() {
545501
accessibleElements = [leftHandleAccessibilityElement, rightHandleAccessibilityElement]
546502
}
@@ -639,6 +595,7 @@ import UIKit
639595
CATransaction.commit()
640596

641597
updateLabelValues()
598+
updateColors()
642599
updateAccessibilityElements()
643600

644601
// update the delegate

0 commit comments

Comments
 (0)