Skip to content

Commit

Permalink
add setupStyle()
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldDownTown committed Mar 16, 2017
1 parent 86abd42 commit c31916a
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 97 deletions.
39 changes: 1 addition & 38 deletions Demo/RangeSeekSliderDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -162,48 +162,12 @@
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gdz-ut-dL8" customClass="RangeSeekSlider" customModule="RangeSeekSlider">
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gdz-ut-dL8" customClass="CustomRangeSeekSlider" customModule="RangeSeekSlider">
<rect key="frame" x="16" y="631" width="343" height="65"/>
<color key="tintColor" red="0.80392158030000005" green="0.80392158030000005" blue="0.80392158030000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="65" id="wa9-1w-B3f"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="step">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="minDistance">
<real key="value" value="1"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="labelsFixed" value="YES"/>
<userDefinedRuntimeAttribute type="color" keyPath="initialColor">
<color key="value" red="0.80392158030000005" green="0.80392158030000005" blue="0.80392158030000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="colorBetweenHandles">
<color key="value" red="0.94509803920000002" green="0.32156862749999998" blue="0.63921568630000003" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="handleColor">
<color key="value" red="0.94509803920000002" green="0.32156862749999998" blue="0.63921568630000003" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="minLabelColor">
<color key="value" red="0.94509803920000002" green="0.32156862749999998" blue="0.63921568630000003" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="maxLabelColor">
<color key="value" red="0.94509803920000002" green="0.32156862749999998" blue="0.63921568630000003" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="minValue">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="maxValue">
<real key="value" value="67"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMinValue">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMaxValue">
<real key="value" value="67"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="By WorldDownTown" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="l8Q-0R-HSN">
<rect key="frame" x="230" y="716" width="128.5" height="17"/>
Expand Down Expand Up @@ -265,7 +229,6 @@
<outlet property="rangeSlider" destination="evq-sI-chw" id="k72-tE-yA5"/>
<outlet property="rangeSliderCurrency" destination="aHO-Yd-TsS" id="hMn-Ta-0mN"/>
<outlet property="rangeSliderCustom" destination="hFS-hr-Hy5" id="3wt-ag-AhN"/>
<outlet property="rangeSliderCustomString" destination="gdz-ut-dL8" id="8C5-lV-cJC"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="YCF-VE-LZM" sceneMemberID="firstResponder"/>
Expand Down
71 changes: 71 additions & 0 deletions Demo/RangeSeekSliderDemo/CustomRangeSeekSlider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// CustomRangeSeekSlider.swift
// RangeSeekSlider
//
// Created by Keisuke Shoji on 2017/03/16.
//
//

import UIKit

@IBDesignable final class CustomRangeSeekSlider: RangeSeekSlider {

fileprivate let prices: [Int] = [
.min, 500, 1_000, 2_000, 3_000, 4_000, 5_000, 6_000, 7_000, 8_000, 9_000,
10_000, 11_000, 12_000, 13_000, 14_000, 15_000, 16_000, 17_000, 18_000, 19_000,
20_000, 21_000, 22_000, 23_000, 24_000, 25_000, 26_000, 27_000, 28_000, 29_000,
30_000, 31_000, 32_000, 33_000, 34_000, 35_000, 36_000, 37_000, 38_000, 39_000,
40_000, 41_000, 42_000, 43_000, 44_000, 45_000, 46_000, 47_000, 48_000, 49_000,
50_000, 55_000, 60_000, 65_000, 70_000, 75_000, 80_000, 85_000, 90_000, 95_000,
100_000, 110_000, 120_000, 130_000, 140_000, 150_000, .max,
]

override func setupStyle() {
let pink: UIColor = #colorLiteral(red: 0.0000000000, green: 0.5019607843, blue: 0.0000000000, alpha: 1) // greenCSS3 #008000
let gray: UIColor = #colorLiteral(red: 0.5019607843, green: 0.5019607843, blue: 0.5019607843, alpha: 1) // gray #808080

minValue = 0.0
maxValue = CGFloat(prices.count - 1)
selectedMinValue = 15.0
selectedMaxValue = CGFloat(prices.count - 1)
minDistance = 1.0
handleColor = pink
minLabelColor = pink
maxLabelColor = pink
colorBetweenHandles = pink
tintColor = gray
numberFormatter.locale = Locale(identifier: "ja_JP")
numberFormatter.numberStyle = .currency
labelsFixed = true
initialColor = gray

delegate = self
}

fileprivate func priceString(value: CGFloat) -> String {
let index: Int = Int(roundf(Float(value)))
let price: Int = prices[index]
if price == .min {
return "Min"
} else if price == .max {
return "Max"
} else {
let priceString: String? = numberFormatter.string(from: price as NSNumber)
return priceString ?? ""
}
}
}


// MARK: - RangeSeekSliderDelegate

extension CustomRangeSeekSlider: RangeSeekSliderDelegate {

func rangeSeekSlider(_ slider: RangeSeekSlider, stringForMinValue minValue: CGFloat) -> String? {
return priceString(value: minValue)
}

func rangeSeekSlider(_ slider: RangeSeekSlider, stringForMaxValue maxValue: CGFloat) -> String? {
return priceString(value: maxValue)
}
}
53 changes: 0 additions & 53 deletions Demo/RangeSeekSliderDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ final class ViewController: UIViewController {
@IBOutlet fileprivate weak var rangeSlider: RangeSeekSlider!
@IBOutlet fileprivate weak var rangeSliderCurrency: RangeSeekSlider!
@IBOutlet fileprivate weak var rangeSliderCustom: RangeSeekSlider!
@IBOutlet fileprivate weak var rangeSliderCustomString: RangeSeekSlider!

fileprivate let prices: [Int] = [
.min, 500, 1_000, 2_000, 3_000, 4_000, 5_000, 6_000, 7_000, 8_000, 9_000,
10_000, 11_000, 12_000, 13_000, 14_000, 15_000, 16_000, 17_000, 18_000, 19_000,
20_000, 21_000, 22_000, 23_000, 24_000, 25_000, 26_000, 27_000, 28_000, 29_000,
30_000, 31_000, 32_000, 33_000, 34_000, 35_000, 36_000, 37_000, 38_000, 39_000,
40_000, 41_000, 42_000, 43_000, 44_000, 45_000, 46_000, 47_000, 48_000, 49_000,
50_000, 55_000, 60_000, 65_000, 70_000, 75_000, 80_000, 85_000, 90_000, 95_000,
100_000, 110_000, 120_000, 130_000, 140_000, 150_000, .max,
]

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -65,36 +54,6 @@ final class ViewController: UIViewController {
rangeSliderCustom.lineHeight = 10.0
rangeSliderCustom.numberFormatter.positivePrefix = "$"
rangeSliderCustom.numberFormatter.positiveSuffix = "M"

// custom label string range slider
rangeSliderCustomString.delegate = self
rangeSliderCustomString.minValue = 0.0
rangeSliderCustomString.maxValue = CGFloat(prices.count - 1)
rangeSliderCustomString.selectedMinValue = 0
rangeSliderCustomString.selectedMaxValue = CGFloat(prices.count - 1)
rangeSliderCustomString.minDistance = 1.0
rangeSliderCustomString.handleColor = #colorLiteral(red: 0.9654981494, green: 0.4302981198, blue: 0.698961854, alpha: 1)
rangeSliderCustomString.minLabelColor = #colorLiteral(red: 0.9654981494, green: 0.4302981198, blue: 0.698961854, alpha: 1)
rangeSliderCustomString.maxLabelColor = #colorLiteral(red: 0.9654981494, green: 0.4302981198, blue: 0.698961854, alpha: 1)
rangeSliderCustomString.colorBetweenHandles = #colorLiteral(red: 0.9654981494, green: 0.4302981198, blue: 0.698961854, alpha: 1)
rangeSliderCustomString.tintColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
rangeSliderCustomString.numberFormatter.locale = Locale.current
rangeSliderCustomString.numberFormatter.numberStyle = .currency
rangeSliderCustomString.labelsFixed = true
rangeSliderCustomString.initialColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
}

fileprivate func priceString(value: CGFloat, numberFormatter: NumberFormatter) -> String {
let index: Int = Int(roundf(Float(value)))
let price: Int = prices[index]
if price == .min {
return "Min"
} else if price == .max {
return "Max"
} else {
let priceString: String? = numberFormatter.string(from: price as NSNumber)
return priceString ?? ""
}
}
}

Expand All @@ -120,16 +79,4 @@ extension ViewController: RangeSeekSliderDelegate {
func didEndTouches(in slider: RangeSeekSlider) {
print("did end touches")
}

func rangeSeekSlider(_ slider: RangeSeekSlider, stringForMinValue minValue: CGFloat) -> String? {
guard slider == rangeSliderCustomString else { return nil }

return priceString(value: minValue, numberFormatter: rangeSliderCustomString.numberFormatter)
}

func rangeSeekSlider(_ slider: RangeSeekSlider, stringForMaxValue maxValue: CGFloat) -> String? {
guard slider == rangeSliderCustomString else { return nil }

return priceString(value: maxValue, numberFormatter: rangeSliderCustomString.numberFormatter)
}
}
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This library is based on [TomThorpe/TTRangeSlider](/~https://github.com/TomThorpe/
![demo](images/demo.gif)

## Demo
You can try on [Appetize.io](https://appetize.io/app/pna3vjqbzjvt0t9c8cgxykg380)
You can try on [Appetize.io](https://appetize.io/app/v3pzcncpffgth3qqe1v8a1ktg4)

or

Expand Down Expand Up @@ -139,6 +139,9 @@ The brief description displayed in accessibility mode for minimum value handler.
#### `maxLabelAccessibilityHint`
The brief description displayed in accessibility mode for maximum value handler. If not set, the default is empty String.

#### `func setupStyle()`
When subclassing `RangeSeekSlider` and setting each item in `setupStyle()`, the design is reflected in Interface Builder as well.

## Requirements
- Swift 3.0+
- iOS 9.0+
Expand All @@ -156,7 +159,7 @@ github "WorldDownTown/RangeSeekSlider"
When installed with Carthage, `@IBDesignable` can't be available ([Carthage Issue](/~https://github.com/Carthage/Carthage/issues/335)). But we have a workaround is shown in [Stack Overflow](http://stackoverflow.com/a/37631860/1835227). It is able to use `@IBDesignable` by subclassing `RangeSeekSlider`.

```swift
@IBDesignable class ChildRangeSeekSlider: RangeSeedSlider {}
@IBDesignable class CustomRangeSeekSlider: RangeSeedSlider {}
```


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.4.1'
s.version = '1.5.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
4 changes: 4 additions & 0 deletions RangeSeekSlider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
CE00D96E1E7AA99A0032CA93 /* CustomRangeSeekSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE00D96D1E7AA99A0032CA93 /* CustomRangeSeekSlider.swift */; };
CE1967FE1E7668A300DBAE72 /* RangeSeekSlider.swift in Headers */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* RangeSeekSlider.swift */; settings = {ATTRIBUTES = (Public, ); }; };
CE2FEE5A1E7A639C00D396B5 /* RangeSeekSliderDelegate.swift in Headers */ = {isa = PBXBuildFile; fileRef = CE3D49B51E712B840071E992 /* RangeSeekSliderDelegate.swift */; settings = {ATTRIBUTES = (Public, ); }; };
CE3D49AF1E7121990071E992 /* RangeSeekSlider.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = OBJ_14 /* RangeSeekSlider.framework */; };
Expand Down Expand Up @@ -56,6 +57,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
CE00D96D1E7AA99A0032CA93 /* CustomRangeSeekSlider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CustomRangeSeekSlider.swift; path = Demo/RangeSeekSliderDemo/CustomRangeSeekSlider.swift; sourceTree = SOURCE_ROOT; };
CE3D49B51E712B840071E992 /* RangeSeekSliderDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RangeSeekSliderDelegate.swift; sourceTree = "<group>"; };
CE3D49B81E71409A0071E992 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
CE9937551E70236800EA982A /* RangeSeekSliderDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RangeSeekSliderDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -121,6 +123,7 @@
CEF273E81E702405003AA995 /* LaunchScreen.storyboard */,
CEF273EA1E702405003AA995 /* Main.storyboard */,
CEF273E61E7023F7003AA995 /* ViewController.swift */,
CE00D96D1E7AA99A0032CA93 /* CustomRangeSeekSlider.swift */,
);
name = RangeSeekSliderDemo;
sourceTree = "<group>";
Expand Down Expand Up @@ -330,6 +333,7 @@
files = (
OBJ_21 /* RangeSeekSlider.swift in Sources */,
CE3D49B61E712B840071E992 /* RangeSeekSliderDelegate.swift in Sources */,
CE00D96E1E7AA99A0032CA93 /* CustomRangeSeekSlider.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
15 changes: 12 additions & 3 deletions Sources/RangeSeekSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import UIKit
setup()
}

public convenience init() {
self.init(frame: .zero)
public convenience init(frame: CGRect = .zero, completion: ((RangeSeekSlider) -> Void)? = nil) {
self.init(frame: frame)
completion?(self)
}


// MARK: - public stored properties
// MARK: - open stored properties

open weak var delegate: RangeSeekSliderDelegate?

Expand Down Expand Up @@ -367,6 +368,12 @@ import UIKit
}


// MARK: - open methods

/// When subclassing **RangeSeekSlider** and setting each item in **setupStyle()**, the design is reflected in Interface Builder as well.
open func setupStyle() {}


// MARK: - private methods

private func setup() {
Expand Down Expand Up @@ -409,6 +416,8 @@ import UIKit
maxLabel.contentsScale = UIScreen.main.scale
layer.addSublayer(maxLabel)

setupStyle()

refresh()
}

Expand Down

0 comments on commit c31916a

Please sign in to comment.