Skip to content

Commit

Permalink
release 0.5.1. Added minimum distance and fixed bug where you would b…
Browse files Browse the repository at this point in the history
…e unable to move sliders if they were on top of each other and at the minimum value. Fixed bug where if the minimum and maximum values were the same it was giving a NaN error. Now returns 0.
  • Loading branch information
Brian Corbin committed Jan 12, 2017
1 parent 83462d6 commit e2d2adc
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 201 deletions.
4 changes: 2 additions & 2 deletions Examples/SwiftRangeSliderExample/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
use_frameworks!

target 'SwiftRangeSliderExample' do
pod 'RappleColorPicker'
pod 'SwiftRangeSlider'
pod 'RappleColorPicker'
pod 'SwiftRangeSlider'
end
12 changes: 8 additions & 4 deletions Examples/SwiftRangeSliderExample/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
PODS:
- RappleColorPicker (1.2.5)
- SwiftRangeSlider (0.2.2)
- SwiftRangeSlider (0.5.0)

DEPENDENCIES:
- RappleColorPicker
- SwiftRangeSlider
- SwiftRangeSlider (from `~/Documents/Programming/Mobile/Pods/SwiftRangeSlider`)

EXTERNAL SOURCES:
SwiftRangeSlider:
:path: ~/Documents/Programming/Mobile/Pods/SwiftRangeSlider

SPEC CHECKSUMS:
RappleColorPicker: a65830bf046b27e45f7d9fcbe8f70c11c85a32d6
SwiftRangeSlider: eba9c63d16027fbe38c7845f0d35a243b96a00bc
SwiftRangeSlider: 97f3b77bf4e1c362f1098800a54a2eddcfafccd4

PODFILE CHECKSUM: 9c89086eec65e07f35c8b5e0bc6f716929b99709
PODFILE CHECKSUM: a6ec3acdb63abddbefce9d0c219ed991b694d743

COCOAPODS: 1.1.1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions Examples/SwiftRangeSliderExample/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

368 changes: 187 additions & 181 deletions Examples/SwiftRangeSliderExample/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion SwiftRangeSlider.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ s.name = "SwiftRangeSlider"
s.summary = "A Swift implementation of a custom UIControl for selecting a range of values on a slider bar."
s.requires_arc = true

s.version = "0.5.0"
s.version = "0.5.1"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Brian Corbin" => "[email protected]" }
s.homepage = "https://github.com/BrianCorbin/SwiftRangeSlider"
Expand Down
Binary file not shown.
49 changes: 45 additions & 4 deletions SwiftRangeSlider/RangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import UIKit
import QuartzCore

enum Knob {
case Neither
case Lower
case Upper
case Both
}

///Class that represents the RangeSlider object.
@IBDesignable open class RangeSlider: UIControl {
Expand All @@ -21,13 +27,21 @@ import QuartzCore
updateLayerFrames()
}
}

///The maximum value selectable on the RangeSlider
@IBInspectable open var maximumValue: Double = 1.0 {
didSet {
updateLayerFrames()
}
}

///The minimum difference in value between the knobs
@IBInspectable open var minimumDistance: Double = 0.0 {
didSet {
updateLayerFrames()
}
}

///The current lower value selected on the RangeSlider
@IBInspectable open var lowerValue: Double = 0.2 {
didSet {
Expand Down Expand Up @@ -97,6 +111,7 @@ import QuartzCore
}

var previousLocation = CGPoint()
var previouslySelectedKnob = Knob.Neither

let trackLayer = RangeSliderTrackLayer()
let lowerThumbLayer = RangeSliderThumbLayer()
Expand Down Expand Up @@ -173,7 +188,15 @@ import QuartzCore
CATransaction.commit()
}


/**
Returns the position of the knob to be placed on the slider given the value it should be on the slider
*/
func positionForValue(_ value: Double) -> Double {
if maximumValue == minimumValue {
return 0
}

return Double(bounds.width - thumbWidth) * (value - minimumValue) /
(maximumValue - minimumValue) + Double(thumbWidth / 2.0)
}
Expand All @@ -190,13 +213,31 @@ import QuartzCore
override open func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
previousLocation = touch.location(in: self)

if lowerThumbLayer.frame.contains(previousLocation) && upperThumbLayer.frame.contains(previousLocation) && (previouslySelectedKnob == Knob.Lower || previouslySelectedKnob == Knob.Neither) {
lowerThumbLayer.highlighted = true
previouslySelectedKnob = Knob.Lower
return true
}

if lowerThumbLayer.frame.contains(previousLocation) && upperThumbLayer.frame.contains(previousLocation) && previouslySelectedKnob == Knob.Upper {
upperThumbLayer.highlighted = true
previouslySelectedKnob = Knob.Upper
return true
}

if lowerThumbLayer.frame.contains(previousLocation) {
lowerThumbLayer.highlighted = true
} else if upperThumbLayer.frame.contains(previousLocation) {
previouslySelectedKnob = Knob.Lower
return true
}

if upperThumbLayer.frame.contains(previousLocation) {
upperThumbLayer.highlighted = true
previouslySelectedKnob = Knob.Upper
return true
}

return lowerThumbLayer.highlighted || upperThumbLayer.highlighted
return false
}

/**
Expand All @@ -215,10 +256,10 @@ import QuartzCore

if lowerThumbLayer.highlighted {
lowerValue += deltaValue
lowerValue = boundValue(lowerValue, toLowerValue: minimumValue, upperValue: upperValue)
lowerValue = boundValue(lowerValue, toLowerValue: minimumValue, upperValue: (upperValue - minimumDistance))
} else if upperThumbLayer.highlighted {
upperValue += deltaValue
upperValue = boundValue(upperValue, toLowerValue: lowerValue, upperValue: maximumValue)
upperValue = boundValue(upperValue, toLowerValue: (lowerValue + minimumDistance), upperValue: maximumValue)
}

sendActions(for: .valueChanged)
Expand Down

0 comments on commit e2d2adc

Please sign in to comment.