Skip to content

Commit

Permalink
add initialColor
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldDownTown committed Mar 14, 2017
1 parent 01a6fb6 commit ab6b621
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 96 deletions.
19 changes: 17 additions & 2 deletions Demo/RangeSeekSliderDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
<userDefinedRuntimeAttribute type="number" keyPath="maxDistance">
<real key="value" value="80"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="minValue">
<real key="value" value="50"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="maxValue">
<real key="value" value="150"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMinValue">
<real key="value" value="60"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMaxValue">
<real key="value" value="140"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<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">
Expand All @@ -88,11 +100,14 @@
<real key="value" value="10"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMinValue">
<real key="value" value="10"/>
<real key="value" value="20"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="colorBetweenHandles">
<color key="value" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMaxValue">
<real key="value" value="80"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<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">
Expand Down Expand Up @@ -161,7 +176,7 @@
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gdz-ut-dL8" customClass="RangeSeekSlider" customModule="RangeSeekSlider">
<rect key="frame" x="16" y="631" width="343" height="65"/>
<color key="tintColor" red="0.86666666670000003" green="0.86666666670000003" blue="0.86666666670000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="tintColor" red="0.40000000000000002" green="0.40000000000000002" blue="0.40000000000000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="65" id="wa9-1w-B3f"/>
</constraints>
Expand Down
1 change: 1 addition & 0 deletions Demo/RangeSeekSliderDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ final class ViewController: UIViewController {
rangeSliderCustomString.tintColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
rangeSliderCustomString.numberFormatter.locale = Locale.current
rangeSliderCustomString.numberFormatter.numberStyle = .currency
rangeSliderCustomString.initialColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
}

fileprivate func priceString(value: CGFloat, numberFormatter: NumberFormatter) -> String {
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ The colorBetweenHandles property sets the color of the line between the two hand
#### `handleBorderColor`
If set it will update the color of the handle borders. Default is `tintColor`.

### `initialColor`
The color of the entire slider when the handle is set to the minimum value and the maximum value. Default is nil.

#### `disableRange`
If true, the control will mimic a normal slider and have only one handle rather than a range.

Expand Down
2 changes: 1 addition & 1 deletion RangeSeekSlider.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'RangeSeekSlider'
s.version = '1.2.0'
s.version = '1.3.0'
s.summary = 'RangeSeedSlider provides a customizable range slider like a UISlider.'
s.description = <<-DESC
RangeSeedSlider provides a customizable range slider like a UISlider.
Expand Down
143 changes: 50 additions & 93 deletions Sources/RangeSeekSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,14 @@ import UIKit
public weak var delegate: RangeSeekSliderDelegate?

/// The minimum possible value to select in the range
@IBInspectable public var minValue: CGFloat = 0.0 {
didSet {
refresh()
}
}
@IBInspectable public var minValue: CGFloat = 0.0

/// The maximum possible value to select in the range
@IBInspectable public var maxValue: CGFloat = 100.0 {
didSet {
refresh()
}
}
@IBInspectable public var maxValue: CGFloat = 100.0

/// The preselected minumum value
/// (note: This should be less than the selectedMaxValue)
@IBInspectable public var selectedMinValue: CGFloat = 10.0 {
@IBInspectable public var selectedMinValue: CGFloat = 0.0 {
didSet {
if selectedMinValue < minValue {
selectedMinValue = minValue
Expand All @@ -59,7 +51,7 @@ import UIKit

/// The preselected maximum value
/// (note: This should be greater than the selectedMinValue)
@IBInspectable public var selectedMaxValue: CGFloat = 90.0 {
@IBInspectable public var selectedMaxValue: CGFloat = 100.0 {
didSet {
if selectedMaxValue > maxValue {
selectedMaxValue = maxValue
Expand Down Expand Up @@ -96,7 +88,6 @@ import UIKit
@IBInspectable public var hideLabels: Bool = false

/// The minimum distance the two selected slider values must be apart. Default is 0.

@IBInspectable public var minDistance: CGFloat = 0.0 {
didSet {
if minDistance < 0.0 {
Expand All @@ -106,7 +97,6 @@ import UIKit
}

/// The maximum distance the two selected slider values must be apart. Default is CGFloat.greatestFiniteMagnitude.

@IBInspectable public var maxDistance: CGFloat = .greatestFiniteMagnitude {
didSet {
if maxDistance < 0.0 {
Expand All @@ -116,41 +106,22 @@ import UIKit
}

/// The color of the minimum value text label. If not set, the default is the tintColor.
@IBInspectable public var minLabelColor: UIColor? {
didSet {
minLabel.foregroundColor = minLabelColor?.cgColor
}
}
@IBInspectable public var minLabelColor: UIColor?

/// The color of the maximum value text label. If not set, the default is the tintColor.
@IBInspectable public var maxLabelColor: UIColor? {
didSet {
maxLabel.foregroundColor = maxLabelColor?.cgColor
}
}
@IBInspectable public var maxLabelColor: UIColor?

/// Handle slider with custom color, you can set custom color for your handle
@IBInspectable public var handleColor: UIColor? {
didSet {
leftHandle.backgroundColor = handleColor?.cgColor
rightHandle.backgroundColor = handleColor?.cgColor
}
}
@IBInspectable public var handleColor: UIColor?

/// Handle slider with custom border color, you can set custom border color for your handle
@IBInspectable public var handleBorderColor: UIColor? {
didSet {
leftHandle.borderColor = handleBorderColor?.cgColor
rightHandle.borderColor = handleBorderColor?.cgColor
}
}
@IBInspectable public var handleBorderColor: UIColor?

/// Set slider line tint color between handles
@IBInspectable public var colorBetweenHandles: UIColor? {
didSet {
sliderLineBetweenHandles.backgroundColor = colorBetweenHandles?.cgColor
}
}
@IBInspectable public var colorBetweenHandles: UIColor?

/// The color of the entire slider when the handle is set to the minimum value and the maximum value. Default is nil.
@IBInspectable public var initialColor: UIColor?

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

rightHandle.frame = startFrame
rightHandle.frame = handleFrame
rightHandle.contents = handleImage?.cgImage
rightHandle.backgroundColor = UIColor.clear.cgColor
}
}

Expand Down Expand Up @@ -285,6 +254,7 @@ import UIKit
if handleTracking == .none {
updateLineHeight()
updateLabelValues()
updateColors()
updateHandlePositions()
updateLabelPositions()
}
Expand All @@ -294,37 +264,6 @@ import UIKit
return CGSize(width: UIViewNoIntrinsicMetric, height: 65.0)
}

open override var tintColor: UIColor! {
didSet {
guard let color: CGColor = tintColor?.cgColor else { return }

CATransaction.begin()
CATransaction.setAnimationDuration(0.5)
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))

sliderLine.backgroundColor = color

if handleColor == nil {
leftHandle.backgroundColor = color
rightHandle.backgroundColor = color
}

if minLabelColor == nil {
minLabel.foregroundColor = color
}

if maxLabelColor == nil {
maxLabel.foregroundColor = color
}

if colorBetweenHandles == nil {
sliderLineBetweenHandles.backgroundColor = color
}

CATransaction.commit()
}
}


// MARK: - UIControl

Expand Down Expand Up @@ -419,25 +358,19 @@ import UIKit
accessibleElements = [leftHandleAccessibilityElement, rightHandleAccessibilityElement]

// draw the slider line
sliderLine.backgroundColor = tintColor.cgColor
layer.addSublayer(sliderLine)

// draw the track distline
sliderLineBetweenHandles.backgroundColor = tintColor.cgColor
layer.addSublayer(sliderLineBetweenHandles)

// draw the minimum slider handle
leftHandle.cornerRadius = handleDiameter / 2.0
leftHandle.backgroundColor = tintColor.cgColor
leftHandle.borderWidth = handleBorderWidth
leftHandle.borderColor = handleBorderColor?.cgColor
layer.addSublayer(leftHandle)

// draw the maximum slider handle
rightHandle.cornerRadius = handleDiameter / 2.0
rightHandle.backgroundColor = tintColor.cgColor
rightHandle.borderWidth = handleBorderWidth
rightHandle.borderColor = handleBorderColor?.cgColor
layer.addSublayer(rightHandle)

let handleFrame: CGRect = CGRect(x: 0.0, y: 0.0, width: handleDiameter, height: handleDiameter)
Expand All @@ -452,22 +385,12 @@ import UIKit
minLabel.alignmentMode = kCAAlignmentCenter
minLabel.frame = labelFrame
minLabel.contentsScale = UIScreen.main.scale
if let cgColor = minLabelColor?.cgColor {
minLabel.foregroundColor = cgColor
} else {
minLabel.foregroundColor = tintColor.cgColor
}
layer.addSublayer(minLabel)

maxLabelFont = UIFont.systemFont(ofSize: labelFontSize)
maxLabel.alignmentMode = kCAAlignmentCenter
maxLabel.frame = labelFrame
maxLabel.contentsScale = UIScreen.main.scale
if let cgColor = maxLabelColor?.cgColor {
maxLabel.foregroundColor = cgColor
} else {
maxLabel.foregroundColor = tintColor.cgColor
}
layer.addSublayer(maxLabel)

refresh()
Expand Down Expand Up @@ -541,6 +464,39 @@ import UIKit
}
}

private func updateColors() {
let isInitial: Bool = selectedMinValue == minValue && selectedMaxValue == maxValue
if let initialColor = initialColor?.cgColor, isInitial {
minLabel.foregroundColor = initialColor
maxLabel.foregroundColor = initialColor
sliderLineBetweenHandles.backgroundColor = initialColor
sliderLine.backgroundColor = initialColor

let color: CGColor = (handleImage == nil) ? initialColor : UIColor.clear.cgColor
leftHandle.backgroundColor = color
leftHandle.borderColor = color
rightHandle.backgroundColor = color
rightHandle.borderColor = color
} else {
let tintCGColor: CGColor = tintColor.cgColor
minLabel.foregroundColor = minLabelColor?.cgColor ?? tintCGColor
maxLabel.foregroundColor = maxLabelColor?.cgColor ?? tintCGColor
sliderLineBetweenHandles.backgroundColor = colorBetweenHandles?.cgColor ?? tintCGColor
sliderLine.backgroundColor = tintCGColor

let color: CGColor
if let _ = handleImage {
color = UIColor.clear.cgColor
} else {
color = handleColor?.cgColor ?? tintCGColor
}
leftHandle.backgroundColor = color
leftHandle.borderColor = color
rightHandle.backgroundColor = color
rightHandle.borderColor = color
}
}

private func updateAccessibilityElements() {
accessibleElements = [leftHandleAccessibilityElement, rightHandleAccessibilityElement]
}
Expand Down Expand Up @@ -639,6 +595,7 @@ import UIKit
CATransaction.commit()

updateLabelValues()
updateColors()
updateAccessibilityElements()

// update the delegate
Expand Down

0 comments on commit ab6b621

Please sign in to comment.