@@ -9,47 +9,47 @@
9import UIKit
9import UIKit
11@IBDesignable public class GMStepper: UIControl {
11@IBDesignable public class GMStepper: UIControl {
13 /// Current value of the stepper. Defaults to 0.
13 /// Current value of the stepper. Defaults to 0.
14 public var value = 0 {
14 public var value = 0 {
16 value = min(maximumValue, max(minimumValue, value))
16 value = min(maximumValue, max(minimumValue, value))
17 label.text = String(value)
17 label.text = String(value)
19 if oldValue != value {
19 if oldValue != value {
20 sendActionsForControlEvents(.ValueChanged)
20 sendActionsForControlEvents(.ValueChanged)
25 /// Minimum value. Must be less than maximumValue. Defaults to 0.
25 /// Minimum value. Must be less than maximumValue. Defaults to 0.
26 @IBInspectable public var minimumValue: Int = 0 {
26 @IBInspectable public var minimumValue: Int = 0 {
32 /// Maximum value. Must be more than minimumValue. Defaults to 10.
32 /// Maximum value. Must be more than minimumValue. Defaults to 10.
33 @IBInspectable public var maximumValue: Int = 10 {
33 @IBInspectable public var maximumValue: Int = 10 {
39 /// Text on the left button. Be sure that it fits in the button. Defaults to "-".
39 /// Text on the left button. Be sure that it fits in the button. Defaults to "-".
40 @IBInspectable public var leftButtonText: String = "-" {
40 @IBInspectable public var leftButtonText: String = "-" {
42 leftButton.setTitle(leftButtonText, forState: .Normal)
42 leftButton.setTitle(leftButtonText, forState: .Normal)
46 /// Text on the right button. Be sure that it fits in the button. Defaults to "+".
46 /// Text on the right button. Be sure that it fits in the button. Defaults to "+".
47 @IBInspectable public var rightButtonText: String = "+" {
47 @IBInspectable public var rightButtonText: String = "+" {
49 rightButton.setTitle(rightButtonText, forState: .Normal)
49 rightButton.setTitle(rightButtonText, forState: .Normal)
53 /// Text color of the buttons. Defaults to white.
53 /// Text color of the buttons. Defaults to white.
54 @IBInspectable public var buttonsTextColor: UIColor = UIColor.whiteColor() {
54 @IBInspectable public var buttonsTextColor: UIColor = UIColor.whiteColor() {
@@ -58,7 +58,7 @@ import UIKit
62 /// Background color of the buttons. Defaults to dark blue.
62 /// Background color of the buttons. Defaults to dark blue.
63 @IBInspectable public var buttonsBackgroundColor: UIColor = UIColor(red:0.21, green:0.5, blue:0.74, alpha:1) {
63 @IBInspectable public var buttonsBackgroundColor: UIColor = UIColor(red:0.21, green:0.5, blue:0.74, alpha:1) {
@@ -68,7 +68,7 @@ import UIKit
68 backgroundColor = buttonsBackgroundColor
68 backgroundColor = buttonsBackgroundColor
72 /// Font of the buttons. Defaults to AvenirNext-Bold, 20.0 points in size.
72 /// Font of the buttons. Defaults to AvenirNext-Bold, 20.0 points in size.
73 public var buttonsFont = UIFont(name: "AvenirNext-Bold", size: 20.0)! {
73 public var buttonsFont = UIFont(name: "AvenirNext-Bold", size: 20.0)! {
@@ -77,14 +77,14 @@ import UIKit
81 /// Text color of the middle label. Defaults to white.
81 /// Text color of the middle label. Defaults to white.
82 @IBInspectable public var labelTextColor: UIColor = UIColor.whiteColor() {
82 @IBInspectable public var labelTextColor: UIColor = UIColor.whiteColor() {
84 label.textColor = labelTextColor
84 label.textColor = labelTextColor
88 /// Text color of the middle label. Defaults to lighter blue.
88 /// Text color of the middle label. Defaults to lighter blue.
89 @IBInspectable public var labelBackgroundColor: UIColor = UIColor(red:0.26, green:0.6, blue:0.87, alpha:1) {
89 @IBInspectable public var labelBackgroundColor: UIColor = UIColor(red:0.26, green:0.6, blue:0.87, alpha:1) {
@@ -98,7 +98,7 @@ import UIKit
98 label.font = labelFont
98 label.font = labelFont
102 /// Corner radius of the stepper's layer. Defaults to 4.0.
102 /// Corner radius of the stepper's layer. Defaults to 4.0.
103 @IBInspectable public var cornerRadius: CGFloat = 4.0 {
103 @IBInspectable public var cornerRadius: CGFloat = 4.0 {
@@ -106,7 +106,7 @@ import UIKit
106 clipsToBounds = true
106 clipsToBounds = true
110 /// 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.
110 /// 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.
111 @IBInspectable public var labelWidthWeight: CGFloat = 0.5 {
111 @IBInspectable public var labelWidthWeight: CGFloat = 0.5 {
@@ -114,35 +114,35 @@ import UIKit
114 setNeedsLayout()
114 setNeedsLayout()
119 Width of the sliding animation. When buttons clicked, the middle label does a slide animation towards to the clicked button. Defaults to 5.
119 Width of the sliding animation. When buttons clicked, the middle label does a slide animation towards to the clicked button. Defaults to 5.
121 private let labelSlideLength: CGFloat = 5
121 private let labelSlideLength: CGFloat = 5
123 /// Duration of the sliding animation
123 /// Duration of the sliding animation
124 private let labelSlideDuration: NSTimeInterval = 0.1
124 private let labelSlideDuration: NSTimeInterval = 0.1
126 /// Duration of the flashing animation when the value hits the limit.
126 /// Duration of the flashing animation when the value hits the limit.
127 private let limitHitAnimationDuration: NSTimeInterval = 0.1
127 private let limitHitAnimationDuration: NSTimeInterval = 0.1
129 /// Color of the flashing animation on the buttons in case the value hit the limit.
129 /// Color of the flashing animation on the buttons in case the value hit the limit.
130 @IBInspectable public var limitHitAnimationColor: UIColor = UIColor(red:0.26, green:0.6, blue:0.87, alpha:1)
130 @IBInspectable public var limitHitAnimationColor: UIColor = UIColor(red:0.26, green:0.6, blue:0.87, alpha:1)
132 private let leftButton = UIButton()
132 private let leftButton = UIButton()
133 private let rightButton = UIButton()
133 private let rightButton = UIButton()
134 private let label = UILabel()
134 private let label = UILabel()
136 required public init(coder aDecoder: NSCoder) {
136 required public init(coder aDecoder: NSCoder) {
137 super.init(coder: aDecoder)
137 super.init(coder: aDecoder)
141 public override init(frame: CGRect) {
141 public override init(frame: CGRect) {
142 super.init(frame: frame)
142 super.init(frame: frame)
146 private func setup() {
146 private func setup() {
147 leftButton.setTitle(leftButtonText, forState: .Normal)
147 leftButton.setTitle(leftButtonText, forState: .Normal)
148 leftButton.setTitleColor(buttonsTextColor, forState: .Normal)
148 leftButton.setTitleColor(buttonsTextColor, forState: .Normal)
@@ -150,35 +150,35 @@ import UIKit
150 leftButton.titleLabel?.font = buttonsFont
150 leftButton.titleLabel?.font = buttonsFont
151 leftButton.addTarget(self, action: "leftButtonTapped:", forControlEvents: .TouchDown)
151 leftButton.addTarget(self, action: "leftButtonTapped:", forControlEvents: .TouchDown)
152 addSubview(leftButton)
152 addSubview(leftButton)
154 rightButton.setTitle(rightButtonText, forState: .Normal)
154 rightButton.setTitle(rightButtonText, forState: .Normal)
155 rightButton.setTitleColor(buttonsTextColor, forState: .Normal)
155 rightButton.setTitleColor(buttonsTextColor, forState: .Normal)
156 rightButton.backgroundColor = buttonsBackgroundColor
156 rightButton.backgroundColor = buttonsBackgroundColor
157 rightButton.titleLabel?.font = buttonsFont
157 rightButton.titleLabel?.font = buttonsFont
158 rightButton.addTarget(self, action: "rightButtonTapped:", forControlEvents: .TouchDown)
158 rightButton.addTarget(self, action: "rightButtonTapped:", forControlEvents: .TouchDown)
159 addSubview(rightButton)
159 addSubview(rightButton)
161 label.textAlignment = .Center
161 label.textAlignment = .Center
162 label.text = String(value)
162 label.text = String(value)
163 label.textColor = labelTextColor
163 label.textColor = labelTextColor
164 label.backgroundColor = labelBackgroundColor
164 label.backgroundColor = labelBackgroundColor
165 label.font = labelFont
165 label.font = labelFont
166 addSubview(label)
166 addSubview(label)
168 backgroundColor = buttonsBackgroundColor
168 backgroundColor = buttonsBackgroundColor
169 layer.cornerRadius = cornerRadius
169 layer.cornerRadius = cornerRadius
170 clipsToBounds = true
170 clipsToBounds = true
173 public override func layoutSubviews() {
173 public override func layoutSubviews() {
174 let buttonWidth = bounds.size.width * ((1 - labelWidthWeight) / 2)
174 let buttonWidth = bounds.size.width * ((1 - labelWidthWeight) / 2)
175 let labelWidth = bounds.size.width * labelWidthWeight
175 let labelWidth = bounds.size.width * labelWidthWeight
177 leftButton.frame = CGRect(x: 0, y: 0, width: buttonWidth, height: bounds.size.height)
177 leftButton.frame = CGRect(x: 0, y: 0, width: buttonWidth, height: bounds.size.height)
178 label.frame = CGRect(x: buttonWidth, y: 0, width: labelWidth, height: bounds.size.height)
178 label.frame = CGRect(x: buttonWidth, y: 0, width: labelWidth, height: bounds.size.height)
179 rightButton.frame = CGRect(x: labelWidth + buttonWidth, y: 0, width: buttonWidth, height: bounds.size.height)
179 rightButton.frame = CGRect(x: labelWidth + buttonWidth, y: 0, width: buttonWidth, height: bounds.size.height)
182 @objc private func leftButtonTapped(button: UIButton) {
182 @objc private func leftButtonTapped(button: UIButton) {
183 if value == minimumValue {
183 if value == minimumValue {
184 animateLimitHitForButton(leftButton)
184 animateLimitHitForButton(leftButton)
@@ -196,7 +196,7 @@ import UIKit
196 animateSlideRight()
196 animateSlideRight()
200 private func animateSlideLeft() {
200 private func animateSlideLeft() {
201 UIView.animateWithDuration(labelSlideDuration, animations: {
201 UIView.animateWithDuration(labelSlideDuration, animations: {
202 self.label.center.x -= self.labelSlideLength
202 self.label.center.x -= self.labelSlideLength
@@ -206,7 +206,7 @@ import UIKit
210 private func animateSlideRight() {
210 private func animateSlideRight() {
211 UIView.animateWithDuration(labelSlideDuration, animations: {
211 UIView.animateWithDuration(labelSlideDuration, animations: {
212 self.label.center.x += self.labelSlideLength
212 self.label.center.x += self.labelSlideLength
@@ -216,7 +216,7 @@ import UIKit
220 private func animateLimitHitForButton(button: UIButton){
220 private func animateLimitHitForButton(button: UIButton){
221 UIView.animateWithDuration(limitHitAnimationDuration, animations: {
221 UIView.animateWithDuration(limitHitAnimationDuration, animations: {
222 button.backgroundColor = self.limitHitAnimationColor
222 button.backgroundColor = self.limitHitAnimationColor