on master help sign in
Merge pull request #26 from Legoless/Swift3

Swift3 Support

commitc13691840913b58e353a990ccecc723ca49d794d authorGunay Mert Karadogan <mertkaradogan@gmail.com> dateTue 4 Oct 2016, 23:18 (10 years ago) parent5d4d5a4f52eb04
3 files changed+104−56
GMStepper/GMStepper.swift+85−54 GMStepperExample/GMStepperExample.xcodeproj/project.pbxproj+18−1 GMStepperExample/GMStepperExample/ViewController.swift+1−1
GMStepper/GMStepper.swift+85−54
@@ -16,15 +16,22 @@ import UIKit
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 }
@@ -55,22 +62,22 @@ import UIKit
5562 /// Text on the left button. Be sure that it fits in the button. Defaults to "−".
5663 @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 "+".
6370 @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+ @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 }
@@ -95,7 +102,7 @@ import UIKit
95102 }
96103
97104 /// Text color of the middle label. Defaults to white.
98− @IBInspectable public var labelTextColor: UIColor = UIColor.whiteColor() {
105+ @IBInspectable public var labelTextColor: UIColor = UIColor.white {
99106 didSet {
100107 label.textColor = labelTextColor
101108 }
@@ -132,10 +139,10 @@ import UIKit
132139 }
133140
134141 /// Color of the border of the stepper and middle label's layer. Defaults to clear color.
135− @IBInspectable public var borderColor: UIColor = UIColor.clearColor() {
142+ @IBInspectable public var borderColor: UIColor = UIColor.clear {
136143 didSet {
137− layer.borderColor = borderColor.CGColor
138− label.layer.borderColor = borderColor.CGColor
144+ layer.borderColor = borderColor.cgColor
145+ label.layer.borderColor = borderColor.cgColor
139146 }
140147 }
141148
@@ -156,38 +163,38 @@ import UIKit
156163 let labelSlideLength: CGFloat = 5
157164
158165 /// Duration of the sliding animation
159− let labelSlideDuration = NSTimeInterval(0.1)
166+ let labelSlideDuration = TimeInterval(0.1)
160167
161168 /// Duration of the animation when the value hits the limit.
162− let limitHitAnimationDuration = NSTimeInterval(0.1)
169+ let limitHitAnimationDuration = TimeInterval(0.1)
163170
164171 lazy var leftButton: UIButton = {
165172 let button = UIButton()
166− button.setTitle(self.leftButtonText, forState: .Normal)
167− button.setTitleColor(self.buttonsTextColor, forState: .Normal)
173+ button.setTitle(self.leftButtonText, for: .normal)
174+ button.setTitleColor(self.buttonsTextColor, for: .normal)
168175 button.backgroundColor = self.buttonsBackgroundColor
169176 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)
177+ button.addTarget(self, action: #selector(GMStepper.leftButtonTouchDown), for: .touchDown)
178+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside)
179+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside)
173180 return button
174181 }()
175182
176183 lazy var rightButton: UIButton = {
177184 let button = UIButton()
178− button.setTitle(self.rightButtonText, forState: .Normal)
179− button.setTitleColor(self.buttonsTextColor, forState: .Normal)
185+ button.setTitle(self.rightButtonText, for: .normal)
186+ button.setTitleColor(self.buttonsTextColor, for: .normal)
180187 button.backgroundColor = self.buttonsBackgroundColor
181188 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)
189+ button.addTarget(self, action: #selector(GMStepper.rightButtonTouchDown), for: .touchDown)
190+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside)
191+ button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside)
185192 return button
186193 }()
187194
188195 lazy var label: UILabel = {
189196 let label = UILabel()
190− label.textAlignment = .Center
197+ label.textAlignment = .center
191198 if self.showIntegerIfDoubleIsInteger && floor(self.value) == self.value {
192199 label.text = String(stringInterpolationSegment: Int(self.value))
193200 } else {
@@ -196,7 +203,7 @@ import UIKit
196203 label.textColor = self.labelTextColor
197204 label.backgroundColor = self.labelBackgroundColor
198205 label.font = self.labelFont
199− label.userInteractionEnabled = true
206+ label.isUserInteractionEnabled = true
200207 let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GMStepper.handlePan))
201208 panRecognizer.maximumNumberOfTouches = 1
202209 label.addGestureRecognizer(panRecognizer)
@@ -225,9 +232,33 @@ import UIKit
225232 }
226233 }
227234 }
235+
236+
237+ public var items : [String] = [] {
238+ didSet {
239+ let isInteger = floor(value) == value
240+
241+ //
242+ // If we have items, we will display them as steps
243+ //
244+
245+ if isInteger && stepValue == 1.0 && items.count > 0 {
246+
247+ var value = Int(self.value)
248+
249+ if value >= items.count {
250+ value = items.count - 1
251+ self.value = Double(value)
252+ }
253+ else {
254+ label.text = items[value]
255+ }
256+ }
257+ }
258+ }
228259
229260 /// Timer used for autorepeat option
230− var timer: NSTimer?
261+ var timer: Timer?
231262
232263 /** When UIStepper reaches its top speed, it alters the value with a time interval of ~0.05 sec.
233264 The user pressing and holding on the stepper repeatedly:
@@ -235,7 +266,7 @@ import UIKit
235266 - For the next 1.5 sec, it changes the value every 0.1 sec.
236267 - Then, every 0.05 sec.
237268 */
238− let timerInterval = NSTimeInterval(0.05)
269+ let timerInterval = TimeInterval(0.05)
239270
240271 /// 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.
241272 var timerFireCount = 0
@@ -268,7 +299,7 @@ import UIKit
268299 layer.cornerRadius = cornerRadius
269300 clipsToBounds = true
270301
271− NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GMStepper.reset), name: UIApplicationWillResignActiveNotification, object: nil)
302+ NotificationCenter.default.addObserver(self, selector: #selector(GMStepper.reset), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
272303 }
273304
274305 public override func layoutSubviews() {
@@ -294,7 +325,7 @@ import UIKit
294325
295326 deinit {
296327 resetTimer()
297− NSNotificationCenter.defaultCenter().removeObserver(self)
328+ NotificationCenter.default.removeObserver(self)
298329 }
299330
300331 /// 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.
@@ -315,15 +346,15 @@ import UIKit
315346extension GMStepper {
316347 func handlePan(gesture: UIPanGestureRecognizer) {
317348 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)
349+ case .began:
350+ leftButton.isEnabled = false
351+ rightButton.isEnabled = false
352+ case .changed:
353+ let translation = gesture.translation(in: label)
354+ gesture.setTranslation(CGPoint.zero, in: label)
324355
325− let slidingRight = gesture.velocityInView(label).x > 0
326− let slidingLeft = gesture.velocityInView(label).x < 0
356+ let slidingRight = gesture.velocity(in: label).x > 0
357+ let slidingLeft = gesture.velocity(in: label).x < 0
327358
328359 // Move the label with pan
329360 if slidingRight {
@@ -356,7 +387,7 @@ extension GMStepper {
356387 self.rightButton.backgroundColor = self.buttonsBackgroundColor
357388 self.leftButton.backgroundColor = self.buttonsBackgroundColor
358389 }
359− case .Ended, .Cancelled, .Failed:
390+ case .ended, .cancelled, .failed:
360391 reset()
361392 default:
362393 break
@@ -368,11 +399,11 @@ extension GMStepper {
368399 stepperState = .Stable
369400 resetTimer()
370401
371− leftButton.enabled = true
372− rightButton.enabled = true
373− label.userInteractionEnabled = true
402+ leftButton.isEnabled = true
403+ rightButton.isEnabled = true
404+ label.isUserInteractionEnabled = true
374405
375− UIView.animateWithDuration(self.labelSlideDuration, animations: {
406+ UIView.animate(withDuration: self.labelSlideDuration, animations: {
376407 self.label.center = self.labelOriginalCenter
377408 self.rightButton.backgroundColor = self.buttonsBackgroundColor
378409 self.leftButton.backgroundColor = self.buttonsBackgroundColor
@@ -383,8 +414,8 @@ extension GMStepper {
383414// MARK: Button Events
384415extension GMStepper {
385416 func leftButtonTouchDown(button: UIButton) {
386− rightButton.enabled = false
387− label.userInteractionEnabled = false
417+ rightButton.isEnabled = false
418+ label.isUserInteractionEnabled = false
388419 resetTimer()
389420
390421 if value == minimumValue {
@@ -397,8 +428,8 @@ extension GMStepper {
397428 }
398429
399430 func rightButtonTouchDown(button: UIButton) {
400− leftButton.enabled = false
401− label.userInteractionEnabled = false
431+ leftButton.isEnabled = false
432+ label.isUserInteractionEnabled = false
402433 resetTimer()
403434
404435 if value == maximumValue {
@@ -418,20 +449,20 @@ extension GMStepper {
418449extension GMStepper {
419450
420451 func animateSlideLeft() {
421− UIView.animateWithDuration(labelSlideDuration) {
452+ UIView.animate(withDuration: labelSlideDuration) {
422453 self.label.center.x -= self.labelSlideLength
423454 }
424455 }
425456
426457 func animateSlideRight() {
427− UIView.animateWithDuration(labelSlideDuration) {
458+ UIView.animate(withDuration: labelSlideDuration) {
428459 self.label.center.x += self.labelSlideLength
429460 }
430461 }
431462
432463 func animateToOriginalPosition() {
433464 if self.label.center != self.labelOriginalCenter {
434− UIView.animateWithDuration(labelSlideDuration) {
465+ UIView.animate(withDuration: labelSlideDuration) {
435466 self.label.center = self.labelOriginalCenter
436467 }
437468 }
@@ -439,14 +470,14 @@ extension GMStepper {
439470
440471 func animateLimitHitIfNeeded() {
441472 if value == minimumValue {
442− animateLimitHitForButton(leftButton)
473+ animateLimitHitForButton(button: leftButton)
443474 } else if value == maximumValue {
444− animateLimitHitForButton(rightButton)
475+ animateLimitHitForButton(button: rightButton)
445476 }
446477 }
447478
448479 func animateLimitHitForButton(button: UIButton){
449− UIView.animateWithDuration(limitHitAnimationDuration) {
480+ UIView.animate(withDuration: limitHitAnimationDuration) {
450481 button.backgroundColor = self.limitHitAnimationColor
451482 }
452483 }
@@ -454,7 +485,7 @@ extension GMStepper {
454485
455486// MARK: Timer
456487extension GMStepper {
457− func handleTimerFire(timer: NSTimer) {
488+ func handleTimerFire(timer: Timer) {
458489 timerFireCount += 1
459490
460491 if timerFireCount % timerFireCountModulo == 0 {
@@ -463,7 +494,7 @@ extension GMStepper {
463494 }
464495
465496 func scheduleTimer() {
466− timer = NSTimer.scheduledTimerWithTimeInterval(timerInterval, target: self, selector: #selector(GMStepper.handleTimerFire), userInfo: nil, repeats: true)
497+ timer = Timer.scheduledTimer(timeInterval: timerInterval, target: self, selector: #selector(GMStepper.handleTimerFire), userInfo: nil, repeats: true)
467498 }
468499
469500 func resetTimer() {
GMStepperExample/GMStepperExample.xcodeproj/project.pbxproj+18−1
@@ -171,14 +171,18 @@
171171 attributes = {
172172 LastSwiftMigration = 0700;
173173 LastSwiftUpdateCheck = 0700;
174− LastUpgradeCheck = 0700;
174+ LastUpgradeCheck = 0800;
175175 ORGANIZATIONNAME = "Gunay Mert Karadogan";
176176 TargetAttributes = {
177177 319C19311B4843EB005EFEE5 = {
178178 CreatedOnToolsVersion = 6.4;
179+ DevelopmentTeam = 289M6XEDV4;
180+ LastSwiftMigration = 0800;
179181 };
180182 319C19461B4843EB005EFEE5 = {
181183 CreatedOnToolsVersion = 6.4;
184+ DevelopmentTeam = 289M6XEDV4;
185+ LastSwiftMigration = 0800;
182186 TestTargetID = 319C19311B4843EB005EFEE5;
183187 };
184188 };
@@ -285,8 +289,10 @@
285289 CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
286290 CLANG_WARN_EMPTY_BODY = YES;
287291 CLANG_WARN_ENUM_CONVERSION = YES;
292+ CLANG_WARN_INFINITE_RECURSION = YES;
288293 CLANG_WARN_INT_CONVERSION = YES;
289294 CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
295+ CLANG_WARN_SUSPICIOUS_MOVE = YES;
290296 CLANG_WARN_UNREACHABLE_CODE = YES;
291297 CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
292298 "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -330,8 +336,10 @@
330336 CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
331337 CLANG_WARN_EMPTY_BODY = YES;
332338 CLANG_WARN_ENUM_CONVERSION = YES;
339+ CLANG_WARN_INFINITE_RECURSION = YES;
333340 CLANG_WARN_INT_CONVERSION = YES;
334341 CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
342+ CLANG_WARN_SUSPICIOUS_MOVE = YES;
335343 CLANG_WARN_UNREACHABLE_CODE = YES;
336344 CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
337345 "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -350,6 +358,7 @@
350358 IPHONEOS_DEPLOYMENT_TARGET = 8.4;
351359 MTL_ENABLE_DEBUG_INFO = NO;
352360 SDKROOT = iphoneos;
361+ SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
353362 VALIDATE_PRODUCT = YES;
354363 };
355364 name = Release;
@@ -358,10 +367,12 @@
358367 isa = XCBuildConfiguration;
359368 buildSettings = {
360369 ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
370+ DEVELOPMENT_TEAM = 289M6XEDV4;
361371 INFOPLIST_FILE = GMStepperExample/Info.plist;
362372 LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
363373 PRODUCT_BUNDLE_IDENTIFIER = "com.gunaymert.$(PRODUCT_NAME:rfc1034identifier)";
364374 PRODUCT_NAME = "$(TARGET_NAME)";
375+ SWIFT_VERSION = 3.0;
365376 };
366377 name = Debug;
367378 };
@@ -369,10 +380,12 @@
369380 isa = XCBuildConfiguration;
370381 buildSettings = {
371382 ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
383+ DEVELOPMENT_TEAM = 289M6XEDV4;
372384 INFOPLIST_FILE = GMStepperExample/Info.plist;
373385 LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
374386 PRODUCT_BUNDLE_IDENTIFIER = "com.gunaymert.$(PRODUCT_NAME:rfc1034identifier)";
375387 PRODUCT_NAME = "$(TARGET_NAME)";
388+ SWIFT_VERSION = 3.0;
376389 };
377390 name = Release;
378391 };
@@ -380,6 +393,7 @@
380393 isa = XCBuildConfiguration;
381394 buildSettings = {
382395 BUNDLE_LOADER = "$(TEST_HOST)";
396+ DEVELOPMENT_TEAM = 289M6XEDV4;
383397 FRAMEWORK_SEARCH_PATHS = (
384398 "$(SDKROOT)/Developer/Library/Frameworks",
385399 "$(inherited)",
@@ -392,6 +406,7 @@
392406 LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
393407 PRODUCT_BUNDLE_IDENTIFIER = "com.gunaymert.$(PRODUCT_NAME:rfc1034identifier)";
394408 PRODUCT_NAME = "$(TARGET_NAME)";
409+ SWIFT_VERSION = 3.0;
395410 TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GMStepperExample.app/GMStepperExample";
396411 };
397412 name = Debug;
@@ -400,6 +415,7 @@
400415 isa = XCBuildConfiguration;
401416 buildSettings = {
402417 BUNDLE_LOADER = "$(TEST_HOST)";
418+ DEVELOPMENT_TEAM = 289M6XEDV4;
403419 FRAMEWORK_SEARCH_PATHS = (
404420 "$(SDKROOT)/Developer/Library/Frameworks",
405421 "$(inherited)",
@@ -408,6 +424,7 @@
408424 LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
409425 PRODUCT_BUNDLE_IDENTIFIER = "com.gunaymert.$(PRODUCT_NAME:rfc1034identifier)";
410426 PRODUCT_NAME = "$(TARGET_NAME)";
427+ SWIFT_VERSION = 3.0;
411428 TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GMStepperExample.app/GMStepperExample";
412429 };
413430 name = Release;
GMStepperExample/GMStepperExample/ViewController.swift+1−1
@@ -13,7 +13,7 @@ class ViewController: UIViewController {
1313 @IBOutlet weak var stepper: GMStepper!
1414 override func viewDidLoad() {
1515 super.viewDidLoad()
16− stepper.addTarget(self, action: #selector(ViewController.stepperValueChanged), forControlEvents: .ValueChanged)
16+ stepper.addTarget(self, action: #selector(ViewController.stepperValueChanged), for: .valueChanged)
1717 }
1818
1919 func stepperValueChanged(stepper: GMStepper) {