@@ -11,72 +11,79 @@ import UIKit
1111@IBDesignable public class GMStepper: UIControl {
1212
1313 /// Current value of the stepper. Defaults to 0.
14− @IBInspectable public var value: Double = 0 {
14+ @objc @IBInspectable public var value: Double = 0 {
1515 didSet {
1616 value = min(maximumValue, max(minimumValue, value))
1717
1818 let isInteger = floor(value) == value
19−
20− if showIntegerIfDoubleIsInteger && isInteger {
19+
20+ //
21+ // If we have items, we will display them as steps
22+ //
23+
24+ if isInteger && stepValue == 1.0 && items.count > 0 {
25+ label.text = items[Int(value)]
26+ }
27+ else if showIntegerIfDoubleIsInteger && isInteger {
2128 label.text = String(stringInterpolationSegment: Int(value))
2229 } else {
2330 label.text = String(stringInterpolationSegment: value)
2431 }
2532
2633 if oldValue != value {
27− sendActionsForControlEvents(.ValueChanged)
34+ sendActions(for: .valueChanged)
2835 }
2936 }
3037 }
3138
3239 /// Minimum value. Must be less than maximumValue. Defaults to 0.
33− @IBInspectable public var minimumValue: Double = 0 {
40+ @objc @IBInspectable public var minimumValue: Double = 0 {
3441 didSet {
3542 value = min(maximumValue, max(minimumValue, value))
3643 }
3744 }
3845
3946 /// Maximum value. Must be more than minimumValue. Defaults to 100.
40− @IBInspectable public var maximumValue: Double = 100 {
47+ @objc @IBInspectable public var maximumValue: Double = 100 {
4148 didSet {
4249 value = min(maximumValue, max(minimumValue, value))
4350 }
4451 }
4552
4653 /// Step/Increment value as in UIStepper. Defaults to 1.
47− @IBInspectable public var stepValue: Double = 1
54+ @objc @IBInspectable public var stepValue: Double = 1
4855
4956 /// The same as UIStepper's autorepeat. If true, holding on the buttons or keeping the pan gesture alters the value repeatedly. Defaults to true.
50− @IBInspectable public var autorepeat: Bool = true
57+ @objc @IBInspectable public var autorepeat: Bool = true
5158
5259 /// If the value is integer, it is shown without floating point.
53− @IBInspectable public var showIntegerIfDoubleIsInteger: Bool = true
60+ @objc @IBInspectable public var showIntegerIfDoubleIsInteger: Bool = true
5461
5562 /// Text on the left button. Be sure that it fits in the button. Defaults to "−".
56− @IBInspectable public var leftButtonText: String = "−" {
63+ @objc @IBInspectable public var leftButtonText: String = "−" {
5764 didSet {
58− leftButton.setTitle(leftButtonText, forState: .Normal)
65+ leftButton.setTitle(leftButtonText, for: .normal)
5966 }
6067 }
6168
6269 /// Text on the right button. Be sure that it fits in the button. Defaults to "+".
63− @IBInspectable public var rightButtonText: String = "+" {
70+ @objc @IBInspectable public var rightButtonText: String = "+" {
6471 didSet {
65− rightButton.setTitle(rightButtonText, forState: .Normal)
72+ rightButton.setTitle(rightButtonText, for: .normal)
6673 }
6774 }
6875
6976 /// Text color of the buttons. Defaults to white.
70− @IBInspectable public var buttonsTextColor: UIColor = UIColor.whiteColor() {
77+ @objc @IBInspectable public var buttonsTextColor: UIColor = UIColor.white {
7178 didSet {
7279 for button in [leftButton, rightButton] {
73− button.setTitleColor(buttonsTextColor, forState: .Normal)
80+ button.setTitleColor(buttonsTextColor, for: .normal)
7481 }
7582 }
7683 }
7784
7885 /// Background color of the buttons. Defaults to dark blue.
79− @IBInspectable public var buttonsBackgroundColor: UIColor = UIColor(red:0.21, green:0.5, blue:0.74, alpha:1) {
86+ @objc @IBInspectable public var buttonsBackgroundColor: UIColor = UIColor(red:0.21, green:0.5, blue:0.74, alpha:1) {
8087 didSet {
8188 for button in [leftButton, rightButton] {
8289 button.backgroundColor = buttonsBackgroundColor
@@ -86,7 +93,7 @@ import UIKit
8693 }
8794
8895 /// Font of the buttons. Defaults to AvenirNext-Bold, 20.0 points in size.
89− public var buttonsFont = UIFont(name: "AvenirNext-Bold", size: 20.0)! {
96+ @objc public var buttonsFont = UIFont(name: "AvenirNext-Bold", size: 20.0)! {
9097 didSet {
9198 for button in [leftButton, rightButton] {
9299 button.titleLabel?.font = buttonsFont
@@ -95,28 +102,35 @@ import UIKit
95102 }
96103
97104 /// Text color of the middle label. Defaults to white.
98− @IBInspectable public var labelTextColor: UIColor = UIColor.whiteColor() {
105+ @objc @IBInspectable public var labelTextColor: UIColor = UIColor.white {
99106 didSet {
100107 label.textColor = labelTextColor
101108 }
102109 }
103110
104111 /// Text color of the middle label. Defaults to lighter blue.
105− @IBInspectable public var labelBackgroundColor: UIColor = UIColor(red:0.26, green:0.6, blue:0.87, alpha:1) {
112+ @objc @IBInspectable public var labelBackgroundColor: UIColor = UIColor(red:0.26, green:0.6, blue:0.87, alpha:1) {
106113 didSet {
107114 label.backgroundColor = labelBackgroundColor
108115 }
109116 }
110117
111118 /// Font of the middle label. Defaults to AvenirNext-Bold, 25.0 points in size.
112− public var labelFont = UIFont(name: "AvenirNext-Bold", size: 25.0)! {
119+ @objc public var labelFont = UIFont(name: "AvenirNext-Bold", size: 25.0)! {
113120 didSet {
114121 label.font = labelFont
115122 }
116123 }
124+ /// Corner radius of the middle label. Defaults to 0.
125+ @objc @IBInspectable public var labelCornerRadius: CGFloat = 0 {
126+ didSet {
127+ label.layer.cornerRadius = labelCornerRadius
128+
129+ }
130+ }
117131
118132 /// Corner radius of the stepper's layer. Defaults to 4.0.
119− @IBInspectable public var cornerRadius: CGFloat = 4.0 {
133+ @objc @IBInspectable public var cornerRadius: CGFloat = 4.0 {
120134 didSet {
121135 layer.cornerRadius = cornerRadius
122136 clipsToBounds = true
@@ -124,7 +138,7 @@ import UIKit
124138 }
125139
126140 /// Border width of the stepper and middle label's layer. Defaults to 0.0.
127− @IBInspectable public var borderWidth: CGFloat = 0.0 {
141+ @objc @IBInspectable public var borderWidth: CGFloat = 0.0 {
128142 didSet {
129143 layer.borderWidth = borderWidth
130144 label.layer.borderWidth = borderWidth
@@ -132,15 +146,15 @@ import UIKit
132146 }
133147
134148 /// Color of the border of the stepper and middle label's layer. Defaults to clear color.
135− @IBInspectable public var borderColor: UIColor = UIColor.clearColor() {
149+ @objc @IBInspectable public var borderColor: UIColor = UIColor.clear {
136150 didSet {
137− layer.borderColor = borderColor.CGColor
138− label.layer.borderColor = borderColor.CGColor
151+ layer.borderColor = borderColor.cgColor
152+ label.layer.borderColor = borderColor.cgColor
139153 }
140154 }
141155
142156 /// Percentage of the middle label's width. Must be between 0 and 1. Defaults to 0.5. Be sure that it is wide enough to show the value.
143− @IBInspectable public var labelWidthWeight: CGFloat = 0.5 {
157+ @objc @IBInspectable public var labelWidthWeight: CGFloat = 0.5 {
144158 didSet {
145159 labelWidthWeight = min(1, max(0, labelWidthWeight))
146160 setNeedsLayout()
@@ -148,7 +162,7 @@ import UIKit
148162 }
149163
150164 /// Color of the flashing animation on the buttons in case the value hit the limit.
151− @IBInspectable public var limitHitAnimationColor: UIColor = UIColor(red:0.26, green:0.6, blue:0.87, alpha:1)
165+ @objc @IBInspectable public var limitHitAnimationColor: UIColor = UIColor(red:0.26, green:0.6, blue:0.87, alpha:1)
152166
153167 /**
154168 Width of the sliding animation. When buttons clicked, the middle label does a slide animation towards to the clicked button. Defaults to 5.
@@ -156,38 +170,40 @@ import UIKit
156170 let labelSlideLength: CGFloat = 5
157171
158172 /// Duration of the sliding animation
159− let labelSlideDuration = NSTimeInterval(0.1)
173+ let labelSlideDuration = TimeInterval(0.1)
160174
161175 /// Duration of the animation when the value hits the limit.
162− let limitHitAnimationDuration = NSTimeInterval(0.1)
176+ let limitHitAnimationDuration = TimeInterval(0.1)
163177
164178 lazy var leftButton: UIButton = {
165179 let button = UIButton()
166− button.setTitle(self.leftButtonText, forState: .Normal)
167− button.setTitleColor(self.buttonsTextColor, forState: .Normal)
180+ button.setTitle(self.leftButtonText, for: .normal)
181+ button.setTitleColor(self.buttonsTextColor, for: .normal)
168182 button.backgroundColor = self.buttonsBackgroundColor
169183 button.titleLabel?.font = self.buttonsFont
170− button.addTarget(self, action: #selector(GMStepper.leftButtonTouchDown), forControlEvents: .TouchDown)
171− button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), forControlEvents: .TouchUpInside)
172− button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), forControlEvents: .TouchUpOutside)
184+ button.addTarget(self, action: #selector(GMStepper.leftButtonTouchDown), for: .touchDown)
185+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside)
186+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside)
187+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchCancel)
173188 return button
174189 }()
175190
176191 lazy var rightButton: UIButton = {
177192 let button = UIButton()
178− button.setTitle(self.rightButtonText, forState: .Normal)
179− button.setTitleColor(self.buttonsTextColor, forState: .Normal)
193+ button.setTitle(self.rightButtonText, for: .normal)
194+ button.setTitleColor(self.buttonsTextColor, for: .normal)
180195 button.backgroundColor = self.buttonsBackgroundColor
181196 button.titleLabel?.font = self.buttonsFont
182− button.addTarget(self, action: #selector(GMStepper.rightButtonTouchDown), forControlEvents: .TouchDown)
183− button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), forControlEvents: .TouchUpInside)
184− button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), forControlEvents: .TouchUpOutside)
197+ button.addTarget(self, action: #selector(GMStepper.rightButtonTouchDown), for: .touchDown)
198+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside)
199+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside)
200+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchCancel)
185201 return button
186202 }()
187203
188204 lazy var label: UILabel = {
189205 let label = UILabel()
190− label.textAlignment = .Center
206+ label.textAlignment = .center
191207 if self.showIntegerIfDoubleIsInteger && floor(self.value) == self.value {
192208 label.text = String(stringInterpolationSegment: Int(self.value))
193209 } else {
@@ -196,7 +212,9 @@ import UIKit
196212 label.textColor = self.labelTextColor
197213 label.backgroundColor = self.labelBackgroundColor
198214 label.font = self.labelFont
199− label.userInteractionEnabled = true
215+ label.layer.cornerRadius = self.labelCornerRadius
216+ label.layer.masksToBounds = true
217+ label.isUserInteractionEnabled = true
200218 let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GMStepper.handlePan))
201219 panRecognizer.maximumNumberOfTouches = 1
202220 label.addGestureRecognizer(panRecognizer)
@@ -225,9 +243,33 @@ import UIKit
225243 }
226244 }
227245 }
246+
247+
248+ @objc public var items : [String] = [] {
249+ didSet {
250+ let isInteger = floor(value) == value
251+
252+ //
253+ // If we have items, we will display them as steps
254+ //
255+
256+ if isInteger && stepValue == 1.0 && items.count > 0 {
257+
258+ var value = Int(self.value)
259+
260+ if value >= items.count {
261+ value = items.count - 1
262+ self.value = Double(value)
263+ }
264+ else {
265+ label.text = items[value]
266+ }
267+ }
268+ }
269+ }
228270
229271 /// Timer used for autorepeat option
230− var timer: NSTimer?
272+ var timer: Timer?
231273
232274 /** When UIStepper reaches its top speed, it alters the value with a time interval of ~0.05 sec.
233275 The user pressing and holding on the stepper repeatedly:
@@ -235,7 +277,7 @@ import UIKit
235277 - For the next 1.5 sec, it changes the value every 0.1 sec.
236278 - Then, every 0.05 sec.
237279 */
238− let timerInterval = NSTimeInterval(0.05)
280+ let timerInterval = TimeInterval(0.05)
239281
240282 /// Check the handleTimerFire: function. While it is counting the number of fires, it decreases the mod value so that the value is altered more frequently.
241283 var timerFireCount = 0
@@ -249,12 +291,12 @@ import UIKit
249291 }
250292 }
251293
252− required public init?(coder aDecoder: NSCoder) {
294+ @objc required public init?(coder aDecoder: NSCoder) {
253295 super.init(coder: aDecoder)
254296 setup()
255297 }
256298
257− public override init(frame: CGRect) {
299+ @objc public override init(frame: CGRect) {
258300 super.init(frame: frame)
259301 setup()
260302 }
@@ -267,8 +309,9 @@ import UIKit
267309 backgroundColor = buttonsBackgroundColor
268310 layer.cornerRadius = cornerRadius
269311 clipsToBounds = true
312+ labelOriginalCenter = label.center
270313
271− NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GMStepper.reset), name: UIApplicationWillResignActiveNotification, object: nil)
314+ NotificationCenter.default.addObserver(self, selector: #selector(GMStepper.reset), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
272315 }
273316
274317 public override func layoutSubviews() {
@@ -294,7 +337,7 @@ import UIKit
294337
295338 deinit {
296339 resetTimer()
297− NSNotificationCenter.defaultCenter().removeObserver(self)
340+ NotificationCenter.default.removeObserver(self)
298341 }
299342
300343 /// Useful closure for logging the timer interval. You can call this in the timer handler to test the autorepeat option. Not used in the current implementation.
@@ -313,17 +356,17 @@ import UIKit
313356
314357// MARK: Pan Gesture
315358extension GMStepper {
316− func handlePan(gesture: UIPanGestureRecognizer) {
359+ @objc func handlePan(gesture: UIPanGestureRecognizer) {
317360 switch gesture.state {
318− case .Began:
319− leftButton.enabled = false
320− rightButton.enabled = false
321− case .Changed:
322− let translation = gesture.translationInView(label)
323− gesture.setTranslation(CGPointZero, inView: label)
361+ case .began:
362+ leftButton.isEnabled = false
363+ rightButton.isEnabled = false
364+ case .changed:
365+ let translation = gesture.translation(in: label)
366+ gesture.setTranslation(CGPoint.zero, in: label)
324367
325− let slidingRight = gesture.velocityInView(label).x > 0
326− let slidingLeft = gesture.velocityInView(label).x < 0
368+ let slidingRight = gesture.velocity(in: label).x > 0
369+ let slidingLeft = gesture.velocity(in: label).x < 0
327370
328371 // Move the label with pan
329372 if slidingRight {
@@ -356,23 +399,23 @@ extension GMStepper {
356399 self.rightButton.backgroundColor = self.buttonsBackgroundColor
357400 self.leftButton.backgroundColor = self.buttonsBackgroundColor
358401 }
359− case .Ended, .Cancelled, .Failed:
402+ case .ended, .cancelled, .failed:
360403 reset()
361404 default:
362405 break
363406 }
364407 }
365408
366− func reset() {
409+ @objc func reset() {
367410 panState = .Stable
368411 stepperState = .Stable
369412 resetTimer()
370413
371− leftButton.enabled = true
372− rightButton.enabled = true
373− label.userInteractionEnabled = true
414+ leftButton.isEnabled = true
415+ rightButton.isEnabled = true
416+ label.isUserInteractionEnabled = true
374417
375− UIView.animateWithDuration(self.labelSlideDuration, animations: {
418+ UIView.animate(withDuration: self.labelSlideDuration, animations: {
376419 self.label.center = self.labelOriginalCenter
377420 self.rightButton.backgroundColor = self.buttonsBackgroundColor
378421 self.leftButton.backgroundColor = self.buttonsBackgroundColor
@@ -382,9 +425,9 @@ extension GMStepper {
382425
383426// MARK: Button Events
384427extension GMStepper {
385− func leftButtonTouchDown(button: UIButton) {
386− rightButton.enabled = false
387− label.userInteractionEnabled = false
428+ @objc func leftButtonTouchDown(button: UIButton) {
429+ rightButton.isEnabled = false
430+ label.isUserInteractionEnabled = false
388431 resetTimer()
389432
390433 if value == minimumValue {
@@ -396,9 +439,9 @@ extension GMStepper {
396439
397440 }
398441
399− func rightButtonTouchDown(button: UIButton) {
400− leftButton.enabled = false
401− label.userInteractionEnabled = false
442+ @objc func rightButtonTouchDown(button: UIButton) {
443+ leftButton.isEnabled = false
444+ label.isUserInteractionEnabled = false
402445 resetTimer()
403446
404447 if value == maximumValue {
@@ -409,7 +452,7 @@ extension GMStepper {
409452 }
410453 }
411454
412− func buttonTouchUp(button: UIButton) {
455+ @objc func buttonTouchUp(button: UIButton) {
413456 reset()
414457 }
415458}
@@ -418,20 +461,20 @@ extension GMStepper {
418461extension GMStepper {
419462
420463 func animateSlideLeft() {
421− UIView.animateWithDuration(labelSlideDuration) {
464+ UIView.animate(withDuration: labelSlideDuration) {
422465 self.label.center.x -= self.labelSlideLength
423466 }
424467 }
425468
426469 func animateSlideRight() {
427− UIView.animateWithDuration(labelSlideDuration) {
470+ UIView.animate(withDuration: labelSlideDuration) {
428471 self.label.center.x += self.labelSlideLength
429472 }
430473 }
431474
432475 func animateToOriginalPosition() {
433476 if self.label.center != self.labelOriginalCenter {
434− UIView.animateWithDuration(labelSlideDuration) {
477+ UIView.animate(withDuration: labelSlideDuration) {
435478 self.label.center = self.labelOriginalCenter
436479 }
437480 }
@@ -439,14 +482,14 @@ extension GMStepper {
439482
440483 func animateLimitHitIfNeeded() {
441484 if value == minimumValue {
442− animateLimitHitForButton(leftButton)
485+ animateLimitHitForButton(button: leftButton)
443486 } else if value == maximumValue {
444− animateLimitHitForButton(rightButton)
487+ animateLimitHitForButton(button: rightButton)
445488 }
446489 }
447490
448491 func animateLimitHitForButton(button: UIButton){
449− UIView.animateWithDuration(limitHitAnimationDuration) {
492+ UIView.animate(withDuration: limitHitAnimationDuration) {
450493 button.backgroundColor = self.limitHitAnimationColor
451494 }
452495 }
@@ -454,7 +497,7 @@ extension GMStepper {
454497
455498// MARK: Timer
456499extension GMStepper {
457− func handleTimerFire(timer: NSTimer) {
500+ @objc func handleTimerFire(timer: Timer) {
458501 timerFireCount += 1
459502
460503 if timerFireCount % timerFireCountModulo == 0 {
@@ -463,7 +506,7 @@ extension GMStepper {
463506 }
464507
465508 func scheduleTimer() {
466− timer = NSTimer.scheduledTimerWithTimeInterval(timerInterval, target: self, selector: #selector(GMStepper.handleTimerFire), userInfo: nil, repeats: true)
509+ timer = Timer.scheduledTimer(timeInterval: timerInterval, target: self, selector: #selector(GMStepper.handleTimerFire), userInfo: nil, repeats: true)
467510 }
468511
469512 func resetTimer() {