on master help sign in
Clear whitespaces. Closes #10.
aa2aa74Gunay Mert Karadogan
GMStepper/GMStepper.swift+30−30
@@ -9,47 +9,47 @@
9import UIKit
9import UIKit
10
10
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 public var value = 0 {
14 public var value = 0 {
15 didSet {
15 didSet {
16 value = min(maximumValue, max(minimumValue, value))
16 value = min(maximumValue, max(minimumValue, value))
17 label.text = String(value)
17 label.text = String(value)
18−
18+
19 if oldValue != value {
19 if oldValue != value {
20 sendActionsForControlEvents(.ValueChanged)
20 sendActionsForControlEvents(.ValueChanged)
21 }
21 }
22 }
22 }
23 }
23 }
24−
24+
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 {
27 didSet {
27 didSet {
28 value = 0
28 value = 0
29 }
29 }
30 }
30 }
31−
31+
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 {
34 didSet {
34 didSet {
35 value = 0
35 value = 0
36 }
36 }
37 }
37 }
38−
38+
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 = "-" {
41 didSet {
41 didSet {
42 leftButton.setTitle(leftButtonText, forState: .Normal)
42 leftButton.setTitle(leftButtonText, forState: .Normal)
43 }
43 }
44 }
44 }
45−
45+
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 = "+" {
48 didSet {
48 didSet {
49 rightButton.setTitle(rightButtonText, forState: .Normal)
49 rightButton.setTitle(rightButtonText, forState: .Normal)
50 }
50 }
51 }
51 }
52−
52+
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() {
55 didSet {
55 didSet {
@@ -58,7 +58,7 @@ import UIKit
58 }
58 }
59 }
59 }
60 }
60 }
61−
61+
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) {
64 didSet {
64 didSet {
@@ -68,7 +68,7 @@ import UIKit
68 backgroundColor = buttonsBackgroundColor
68 backgroundColor = buttonsBackgroundColor
69 }
69 }
70 }
70 }
71−
71+
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)! {
74 didSet {
74 didSet {
@@ -77,14 +77,14 @@ import UIKit
77 }
77 }
78 }
78 }
79 }
79 }
80−
80+
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() {
83 didSet {
83 didSet {
84 label.textColor = labelTextColor
84 label.textColor = labelTextColor
85 }
85 }
86 }
86 }
87−
87+
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) {
90 didSet {
90 didSet {
@@ -98,7 +98,7 @@ import UIKit
98 label.font = labelFont
98 label.font = labelFont
99 }
99 }
100 }
100 }
101−
101+
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 {
104 didSet {
104 didSet {
@@ -106,7 +106,7 @@ import UIKit
106 clipsToBounds = true
106 clipsToBounds = true
107 }
107 }
108 }
108 }
109−
109+
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 {
112 didSet {
112 didSet {
@@ -114,35 +114,35 @@ import UIKit
114 setNeedsLayout()
114 setNeedsLayout()
115 }
115 }
116 }
116 }
117−
117+
118 /**
118 /**
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.
120 */
120 */
121 private let labelSlideLength: CGFloat = 5
121 private let labelSlideLength: CGFloat = 5
122−
122+
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
125−
125+
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
128−
128+
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)
131−
131+
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()
135−
135+
136 required public init(coder aDecoder: NSCoder) {
136 required public init(coder aDecoder: NSCoder) {
137 super.init(coder: aDecoder)
137 super.init(coder: aDecoder)
138 setup()
138 setup()
139 }
139 }
140−
140+
141 public override init(frame: CGRect) {
141 public override init(frame: CGRect) {
142 super.init(frame: frame)
142 super.init(frame: frame)
143 setup()
143 setup()
144 }
144 }
145−
145+
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)
153−
153+
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)
160−
160+
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)
167−
167+
168 backgroundColor = buttonsBackgroundColor
168 backgroundColor = buttonsBackgroundColor
169 layer.cornerRadius = cornerRadius
169 layer.cornerRadius = cornerRadius
170 clipsToBounds = true
170 clipsToBounds = true
171 }
171 }
172−
172+
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
176−
176+
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)
180 }
180 }
181−
181+
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()
197 }
197 }
198 }
198 }
199−
199+
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
206 })
206 })
207 })
207 })
208 }
208 }
209−
209+
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
216 })
216 })
217 })
217 })
218 }
218 }
219−
219+
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
GMStepperExample/GMStepperExample/ViewController.swift+1−2
@@ -15,9 +15,8 @@ class ViewController: UIViewController {
15 super.viewDidLoad()
15 super.viewDidLoad()
16 stepper.addTarget(self, action: "stepperValueChanged:", forControlEvents: .ValueChanged)
16 stepper.addTarget(self, action: "stepperValueChanged:", forControlEvents: .ValueChanged)
17 }
17 }
18−
18+
19 func stepperValueChanged(stepper: GMStepper) {
19 func stepperValueChanged(stepper: GMStepper) {
20 println(stepper.value)
20 println(stepper.value)
21 }
21 }
22}
22}
23−