Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions RangeSlider/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097.3" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -20,6 +19,23 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="AsD-zw-Yxv" customClass="RangeSlider" customModule="RangeSlider" customModuleProvider="target">
<rect key="frame" x="50" y="488" width="275" height="30"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="bqb-l3-6NV"/>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="DDu-bb-Jot" customClass="RangeSlider" customModule="RangeSlider" customModuleProvider="target">
<rect key="frame" x="50" y="526" width="275" height="30"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="a8H-bl-Tgg"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="sticky" value="YES"/>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="kQO-Yc-D8F" customClass="RangeSlider" customModule="RangeSlider" customModuleProvider="target">
<rect key="frame" x="67.5" y="566" width="240" height="43"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand Down Expand Up @@ -54,21 +70,17 @@
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="AsD-zw-Yxv" customClass="RangeSlider" customModule="RangeSlider" customModuleProvider="target">
<rect key="frame" x="50" y="516" width="275" height="30"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="bqb-l3-6NV"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="kQO-Yc-D8F" firstAttribute="top" secondItem="AsD-zw-Yxv" secondAttribute="bottom" constant="20" id="345-Gp-Xga"/>
<constraint firstItem="AsD-zw-Yxv" firstAttribute="leading" secondItem="DDu-bb-Jot" secondAttribute="leading" id="6iR-oc-23s"/>
<constraint firstItem="kQO-Yc-D8F" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="H1I-SU-ViG"/>
<constraint firstItem="AsD-zw-Yxv" firstAttribute="trailing" secondItem="DDu-bb-Jot" secondAttribute="trailing" id="K40-co-Txl"/>
<constraint firstAttribute="trailing" secondItem="AsD-zw-Yxv" secondAttribute="trailing" constant="50" id="M26-Ns-STa"/>
<constraint firstItem="DDu-bb-Jot" firstAttribute="top" secondItem="AsD-zw-Yxv" secondAttribute="bottom" constant="8" id="Oze-3f-fU9"/>
<constraint firstItem="AsD-zw-Yxv" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" constant="50" id="ZSF-Ee-JQM"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="kQO-Yc-D8F" secondAttribute="bottom" constant="58" id="bL8-88-isy"/>
<constraint firstItem="kQO-Yc-D8F" firstAttribute="top" secondItem="DDu-bb-Jot" secondAttribute="bottom" constant="10" id="vNv-Xf-mBo"/>
</constraints>
</view>
</viewController>
Expand Down
11 changes: 11 additions & 0 deletions RangeSlider/RangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public class RangeSlider: UIControl {
}
}

@IBInspectable public var sticky: Bool = false

var gapBetweenThumbs: Double {
return 0.5 * Double(thumbWidth) * (maximumValue - minimumValue) / Double(bounds.width)
}
Expand Down Expand Up @@ -277,8 +279,17 @@ public class RangeSlider: UIControl {
// Update the values
if lowerThumbLayer.highlighted {
lowerValue = boundValue(lowerValue + deltaValue, toLowerValue: minimumValue, upperValue: upperValue - gapBetweenThumbs)
if(self.sticky)
{
upperValue = boundValue(upperValue + deltaValue, toLowerValue: lowerValue - gapBetweenThumbs, upperValue: maximumValue)
}

} else if upperThumbLayer.highlighted {
upperValue = boundValue(upperValue + deltaValue, toLowerValue: lowerValue + gapBetweenThumbs, upperValue: maximumValue)
if(self.sticky)
{
lowerValue = boundValue(lowerValue + deltaValue, toLowerValue: minimumValue, upperValue: upperValue + gapBetweenThumbs)
}
}

sendActions(for: .valueChanged)
Expand Down
15 changes: 10 additions & 5 deletions RangeSlider/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,31 @@ import UIKit
class ViewController: UIViewController {
let rangeSlider1 = RangeSlider(frame: CGRect.zero)
let rangeSlider2 = RangeSlider(frame: CGRect.zero)
let rangeSlider3 = RangeSlider(frame: CGRect.zero)

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
rangeSlider2.trackHighlightTintColor = UIColor.red
rangeSlider2.curvaceousness = 0.0

rangeSlider3.trackHighlightTintColor = UIColor.red
rangeSlider3.curvaceousness = 0.0
rangeSlider2.sticky = true
view.addSubview(rangeSlider1)
view.addSubview(rangeSlider2)
view.addSubview(rangeSlider3)

rangeSlider1.addTarget(self, action: #selector(ViewController.rangeSliderValueChanged(_:)), for: .valueChanged)
rangeSlider2.addTarget(self, action: #selector(ViewController.rangeSliderValueChanged(_:)), for: .valueChanged)
}

override func viewDidLayoutSubviews() {
let margin: CGFloat = 20.0
let width = view.bounds.width - 2.0 * margin
rangeSlider1.frame = CGRect(x: margin, y: margin + topLayoutGuide.length + 100,
width: width, height: 31.0)
rangeSlider2.frame = CGRect(x: margin + 20, y: 5 * margin + topLayoutGuide.length + 100,
width: width - 40, height: 40)
rangeSlider2.frame = CGRect(x: margin, y: 5 * margin + topLayoutGuide.length + 100,
width: width, height: 31.0)
rangeSlider3.frame = CGRect(x: margin + 20, y: 10 * margin + topLayoutGuide.length + 100,
width: width - 40, height: 40)
}

override func didReceiveMemoryWarning() {
Expand Down