on master help sign in
Add value:Double, stepperValue and showIntegerIfDoubleIsInteger
1e5a91fGunay Mert Karadogan
GMStepper/GMStepper.swift+24−8
@@ -11,10 +11,17 @@ import UIKit
11@IBDesignable public class GMStepper: UIControl {
11@IBDesignable public class GMStepper: UIControl {
12
12
13 /// Current value of the stepper. Defaults to 0.
13 /// Current value of the stepper. Defaults to 0.
14− @IBInspectable public var value = 0 {
14+ @IBInspectable public var value: Double = 0 {
15 didSet {
15 didSet {
16 value = min(maximumValue, max(minimumValue, value))
16 value = min(maximumValue, max(minimumValue, value))
17+
18+ let isInteger = floor(value) == value
19+
20+ if showIntegerIfDoubleIsInteger && isInteger {
21+ label.text = String(stringInterpolationSegment: Int(value))
22+ } else {
17− label.text = String(value)
23+ label.text = String(stringInterpolationSegment: value)
24+ }
18
25
19 if oldValue != value {
26 if oldValue != value {
20 sendActionsForControlEvents(.ValueChanged)
27 sendActionsForControlEvents(.ValueChanged)
@@ -23,22 +30,27 @@ import UIKit
23 }
30 }
24
31
25 /// Minimum value. Must be less than maximumValue. Defaults to 0.
32 /// Minimum value. Must be less than maximumValue. Defaults to 0.
26− @IBInspectable public var minimumValue: Int = 0 {
33+ @IBInspectable public var minimumValue: Double = 0 {
27 didSet {
34 didSet {
28 value = min(maximumValue, max(minimumValue, value))
35 value = min(maximumValue, max(minimumValue, value))
29 }
36 }
30 }
37 }
31
38
32− /// Maximum value. Must be more than minimumValue. Defaults to 10.
39+ /// Maximum value. Must be more than minimumValue. Defaults to 100.
33− @IBInspectable public var maximumValue: Int = 10 {
40+ @IBInspectable public var maximumValue: Double = 100 {
34 didSet {
41 didSet {
35 value = min(maximumValue, max(minimumValue, value))
42 value = min(maximumValue, max(minimumValue, value))
36 }
43 }
37 }
44 }
38
45
46+ /// Step/Increment value as in UIStepper. Defaults to 1.
47+ @IBInspectable public var stepValue: Double = 1
48+
39 /// The same as UIStepper's autorepeat. If true, holding on the buttons or keeping the pan gesture alters the value repeatedly. Defaults to true.
49 /// The same as UIStepper's autorepeat. If true, holding on the buttons or keeping the pan gesture alters the value repeatedly. Defaults to true.
40 @IBInspectable public var autorepeat: Bool = true
50 @IBInspectable public var autorepeat: Bool = true
41
51
52+ @IBInspectable public var showIntegerIfDoubleIsInteger: Bool = true
53+
42 /// Text on the left button. Be sure that it fits in the button. Defaults to "-".
54 /// Text on the left button. Be sure that it fits in the button. Defaults to "-".
43 @IBInspectable public var leftButtonText: String = "-" {
55 @IBInspectable public var leftButtonText: String = "-" {
44 didSet {
56 didSet {
@@ -159,7 +171,11 @@ import UIKit
159 lazy var label: UILabel = {
171 lazy var label: UILabel = {
160 let label = UILabel()
172 let label = UILabel()
161 label.textAlignment = .Center
173 label.textAlignment = .Center
174+ if self.showIntegerIfDoubleIsInteger && floor(self.value) == self.value {
162− label.text = String(self.value)
175+ label.text = String(stringInterpolationSegment: Int(self.value))
176+ } else {
177+ label.text = String(stringInterpolationSegment: self.value)
178+ }
163 label.textColor = self.labelTextColor
179 label.textColor = self.labelTextColor
164 label.backgroundColor = self.labelBackgroundColor
180 label.backgroundColor = self.labelBackgroundColor
165 label.font = self.labelFont
181 label.font = self.labelFont
@@ -264,9 +280,9 @@ import UIKit
264
280
265 func updateValue() {
281 func updateValue() {
266 if stepperState == .ShouldIncrease {
282 if stepperState == .ShouldIncrease {
267− value += 1
283+ value += stepValue
268 } else if stepperState == .ShouldDecrease {
284 } else if stepperState == .ShouldDecrease {
269− value -= 1
285+ value -= stepValue
270 }
286 }
271 }
287 }
272}
288}
GMStepperExample/GMStepperExample/Base.lproj/Main.storyboard+12−9
@@ -25,11 +25,17 @@
25 </constraints>
25 </constraints>
26 <userDefinedRuntimeAttributes>
26 <userDefinedRuntimeAttributes>
27 <userDefinedRuntimeAttribute type="number" keyPath="minimumValue">
27 <userDefinedRuntimeAttribute type="number" keyPath="minimumValue">
28− <integer key="value" value="0"/>
28+ <real key="value" value="2"/>
29 </userDefinedRuntimeAttribute>
29 </userDefinedRuntimeAttribute>
30 <userDefinedRuntimeAttribute type="number" keyPath="maximumValue">
30 <userDefinedRuntimeAttribute type="number" keyPath="maximumValue">
31 <integer key="value" value="100"/>
31 <integer key="value" value="100"/>
32 </userDefinedRuntimeAttribute>
32 </userDefinedRuntimeAttribute>
33+ <userDefinedRuntimeAttribute type="number" keyPath="stepValue">
34+ <real key="value" value="0.5"/>
35+ </userDefinedRuntimeAttribute>
36+ <userDefinedRuntimeAttribute type="number" keyPath="value">
37+ <real key="value" value="2.1000000000000001"/>
38+ </userDefinedRuntimeAttribute>
33 </userDefinedRuntimeAttributes>
39 </userDefinedRuntimeAttributes>
34 </view>
40 </view>
35 <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="if7-U5-bAM" customClass="GMStepper" customModule="GMStepperExample" customModuleProvider="target">
41 <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="if7-U5-bAM" customClass="GMStepper" customModule="GMStepperExample" customModuleProvider="target">
@@ -41,10 +47,10 @@
41 </constraints>
47 </constraints>
42 <userDefinedRuntimeAttributes>
48 <userDefinedRuntimeAttributes>
43 <userDefinedRuntimeAttribute type="number" keyPath="minimumValue">
49 <userDefinedRuntimeAttribute type="number" keyPath="minimumValue">
44− <integer key="value" value="0"/>
50+ <real key="value" value="0.40000000000000002"/>
45 </userDefinedRuntimeAttribute>
51 </userDefinedRuntimeAttribute>
46 <userDefinedRuntimeAttribute type="number" keyPath="maximumValue">
52 <userDefinedRuntimeAttribute type="number" keyPath="maximumValue">
47− <integer key="value" value="100"/>
53+ <real key="value" value="100"/>
48 </userDefinedRuntimeAttribute>
54 </userDefinedRuntimeAttribute>
49 <userDefinedRuntimeAttribute type="color" keyPath="buttonsBackgroundColor">
55 <userDefinedRuntimeAttribute type="color" keyPath="buttonsBackgroundColor">
50 <color key="value" red="1" green="0.3591497541" blue="0.36842127060000002" alpha="1" colorSpace="calibratedRGB"/>
56 <color key="value" red="1" green="0.3591497541" blue="0.36842127060000002" alpha="1" colorSpace="calibratedRGB"/>
@@ -61,6 +67,9 @@
61 <userDefinedRuntimeAttribute type="color" keyPath="limitHitAnimationColor">
67 <userDefinedRuntimeAttribute type="color" keyPath="limitHitAnimationColor">
62 <color key="value" red="0.90823972230000005" green="0.92638683320000004" blue="0.93171715740000005" alpha="1" colorSpace="calibratedRGB"/>
68 <color key="value" red="0.90823972230000005" green="0.92638683320000004" blue="0.93171715740000005" alpha="1" colorSpace="calibratedRGB"/>
63 </userDefinedRuntimeAttribute>
69 </userDefinedRuntimeAttribute>
70+ <userDefinedRuntimeAttribute type="number" keyPath="stepValue">
71+ <real key="value" value="0.40000000000000002"/>
72+ </userDefinedRuntimeAttribute>
64 </userDefinedRuntimeAttributes>
73 </userDefinedRuntimeAttributes>
65 </view>
74 </view>
66 <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="R9n-I0-fq7" customClass="GMStepper" customModule="GMStepperExample" customModuleProvider="target">
75 <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="R9n-I0-fq7" customClass="GMStepper" customModule="GMStepperExample" customModuleProvider="target">
@@ -83,12 +92,6 @@
83 <userDefinedRuntimeAttribute type="color" keyPath="buttonsTextColor">
92 <userDefinedRuntimeAttribute type="color" keyPath="buttonsTextColor">
84 <color key="value" white="1" alpha="1" colorSpace="calibratedWhite"/>
93 <color key="value" white="1" alpha="1" colorSpace="calibratedWhite"/>
85 </userDefinedRuntimeAttribute>
94 </userDefinedRuntimeAttribute>
86− <userDefinedRuntimeAttribute type="number" keyPath="minimumValue">
87− <integer key="value" value="0"/>
88− </userDefinedRuntimeAttribute>
89− <userDefinedRuntimeAttribute type="number" keyPath="maximumValue">
90− <integer key="value" value="100"/>
91− </userDefinedRuntimeAttribute>
92 <userDefinedRuntimeAttribute type="color" keyPath="limitHitAnimationColor">
95 <userDefinedRuntimeAttribute type="color" keyPath="limitHitAnimationColor">
93 <color key="value" red="0.6588235294" green="0.85882352939999995" blue="0.6588235294" alpha="1" colorSpace="calibratedRGB"/>
96 <color key="value" red="0.6588235294" green="0.85882352939999995" blue="0.6588235294" alpha="1" colorSpace="calibratedRGB"/>
94 </userDefinedRuntimeAttribute>
97 </userDefinedRuntimeAttribute>