Converted to Swift3

This commit is contained in:
Dal Rupnik 2016-08-18 17:29:49 +02:00
parent 5d4d5a4989
commit 43d38b5973
3 changed files with 71 additions and 54 deletions

View File

@ -24,7 +24,7 @@ import UIKit
}
if oldValue != value {
sendActionsForControlEvents(.ValueChanged)
sendActions(for: .valueChanged)
}
}
}
@ -55,22 +55,22 @@ import UIKit
/// Text on the left button. Be sure that it fits in the button. Defaults to "".
@IBInspectable public var leftButtonText: String = "" {
didSet {
leftButton.setTitle(leftButtonText, forState: .Normal)
leftButton.setTitle(leftButtonText, for: .normal)
}
}
/// Text on the right button. Be sure that it fits in the button. Defaults to "+".
@IBInspectable public var rightButtonText: String = "+" {
didSet {
rightButton.setTitle(rightButtonText, forState: .Normal)
rightButton.setTitle(rightButtonText, for: .normal)
}
}
/// Text color of the buttons. Defaults to white.
@IBInspectable public var buttonsTextColor: UIColor = UIColor.whiteColor() {
@IBInspectable public var buttonsTextColor: UIColor = UIColor.white {
didSet {
for button in [leftButton, rightButton] {
button.setTitleColor(buttonsTextColor, forState: .Normal)
button.setTitleColor(buttonsTextColor, for: .normal)
}
}
}
@ -95,7 +95,7 @@ import UIKit
}
/// Text color of the middle label. Defaults to white.
@IBInspectable public var labelTextColor: UIColor = UIColor.whiteColor() {
@IBInspectable public var labelTextColor: UIColor = UIColor.white {
didSet {
label.textColor = labelTextColor
}
@ -132,10 +132,10 @@ import UIKit
}
/// Color of the border of the stepper and middle label's layer. Defaults to clear color.
@IBInspectable public var borderColor: UIColor = UIColor.clearColor() {
@IBInspectable public var borderColor: UIColor = UIColor.clear {
didSet {
layer.borderColor = borderColor.CGColor
label.layer.borderColor = borderColor.CGColor
layer.borderColor = borderColor.cgColor
label.layer.borderColor = borderColor.cgColor
}
}
@ -156,38 +156,38 @@ import UIKit
let labelSlideLength: CGFloat = 5
/// Duration of the sliding animation
let labelSlideDuration = NSTimeInterval(0.1)
let labelSlideDuration = TimeInterval(0.1)
/// Duration of the animation when the value hits the limit.
let limitHitAnimationDuration = NSTimeInterval(0.1)
let limitHitAnimationDuration = TimeInterval(0.1)
lazy var leftButton: UIButton = {
let button = UIButton()
button.setTitle(self.leftButtonText, forState: .Normal)
button.setTitleColor(self.buttonsTextColor, forState: .Normal)
button.setTitle(self.leftButtonText, for: .normal)
button.setTitleColor(self.buttonsTextColor, for: .normal)
button.backgroundColor = self.buttonsBackgroundColor
button.titleLabel?.font = self.buttonsFont
button.addTarget(self, action: #selector(GMStepper.leftButtonTouchDown), forControlEvents: .TouchDown)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), forControlEvents: .TouchUpInside)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), forControlEvents: .TouchUpOutside)
button.addTarget(self, action: #selector(GMStepper.leftButtonTouchDown), for: .touchDown)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside)
return button
}()
lazy var rightButton: UIButton = {
let button = UIButton()
button.setTitle(self.rightButtonText, forState: .Normal)
button.setTitleColor(self.buttonsTextColor, forState: .Normal)
button.setTitle(self.rightButtonText, for: .normal)
button.setTitleColor(self.buttonsTextColor, for: .normal)
button.backgroundColor = self.buttonsBackgroundColor
button.titleLabel?.font = self.buttonsFont
button.addTarget(self, action: #selector(GMStepper.rightButtonTouchDown), forControlEvents: .TouchDown)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), forControlEvents: .TouchUpInside)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), forControlEvents: .TouchUpOutside)
button.addTarget(self, action: #selector(GMStepper.rightButtonTouchDown), for: .touchDown)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside)
return button
}()
lazy var label: UILabel = {
let label = UILabel()
label.textAlignment = .Center
label.textAlignment = .center
if self.showIntegerIfDoubleIsInteger && floor(self.value) == self.value {
label.text = String(stringInterpolationSegment: Int(self.value))
} else {
@ -196,7 +196,7 @@ import UIKit
label.textColor = self.labelTextColor
label.backgroundColor = self.labelBackgroundColor
label.font = self.labelFont
label.userInteractionEnabled = true
label.isUserInteractionEnabled = true
let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GMStepper.handlePan))
panRecognizer.maximumNumberOfTouches = 1
label.addGestureRecognizer(panRecognizer)
@ -227,7 +227,7 @@ import UIKit
}
/// Timer used for autorepeat option
var timer: NSTimer?
var timer: Timer?
/** When UIStepper reaches its top speed, it alters the value with a time interval of ~0.05 sec.
The user pressing and holding on the stepper repeatedly:
@ -235,7 +235,7 @@ import UIKit
- For the next 1.5 sec, it changes the value every 0.1 sec.
- Then, every 0.05 sec.
*/
let timerInterval = NSTimeInterval(0.05)
let timerInterval = TimeInterval(0.05)
/// 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.
var timerFireCount = 0
@ -268,7 +268,7 @@ import UIKit
layer.cornerRadius = cornerRadius
clipsToBounds = true
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GMStepper.reset), name: UIApplicationWillResignActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(GMStepper.reset), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
}
public override func layoutSubviews() {
@ -294,7 +294,7 @@ import UIKit
deinit {
resetTimer()
NSNotificationCenter.defaultCenter().removeObserver(self)
NotificationCenter.default.removeObserver(self)
}
/// 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 +315,15 @@ import UIKit
extension GMStepper {
func handlePan(gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .Began:
leftButton.enabled = false
rightButton.enabled = false
case .Changed:
let translation = gesture.translationInView(label)
gesture.setTranslation(CGPointZero, inView: label)
case .began:
leftButton.isEnabled = false
rightButton.isEnabled = false
case .changed:
let translation = gesture.translation(in: label)
gesture.setTranslation(CGPoint.zero, in: label)
let slidingRight = gesture.velocityInView(label).x > 0
let slidingLeft = gesture.velocityInView(label).x < 0
let slidingRight = gesture.velocity(in: label).x > 0
let slidingLeft = gesture.velocity(in: label).x < 0
// Move the label with pan
if slidingRight {
@ -356,7 +356,7 @@ extension GMStepper {
self.rightButton.backgroundColor = self.buttonsBackgroundColor
self.leftButton.backgroundColor = self.buttonsBackgroundColor
}
case .Ended, .Cancelled, .Failed:
case .ended, .cancelled, .failed:
reset()
default:
break
@ -368,11 +368,11 @@ extension GMStepper {
stepperState = .Stable
resetTimer()
leftButton.enabled = true
rightButton.enabled = true
label.userInteractionEnabled = true
leftButton.isEnabled = true
rightButton.isEnabled = true
label.isUserInteractionEnabled = true
UIView.animateWithDuration(self.labelSlideDuration, animations: {
UIView.animate(withDuration: self.labelSlideDuration, animations: {
self.label.center = self.labelOriginalCenter
self.rightButton.backgroundColor = self.buttonsBackgroundColor
self.leftButton.backgroundColor = self.buttonsBackgroundColor
@ -383,8 +383,8 @@ extension GMStepper {
// MARK: Button Events
extension GMStepper {
func leftButtonTouchDown(button: UIButton) {
rightButton.enabled = false
label.userInteractionEnabled = false
rightButton.isEnabled = false
label.isUserInteractionEnabled = false
resetTimer()
if value == minimumValue {
@ -397,8 +397,8 @@ extension GMStepper {
}
func rightButtonTouchDown(button: UIButton) {
leftButton.enabled = false
label.userInteractionEnabled = false
leftButton.isEnabled = false
label.isUserInteractionEnabled = false
resetTimer()
if value == maximumValue {
@ -418,20 +418,20 @@ extension GMStepper {
extension GMStepper {
func animateSlideLeft() {
UIView.animateWithDuration(labelSlideDuration) {
UIView.animate(withDuration: labelSlideDuration) {
self.label.center.x -= self.labelSlideLength
}
}
func animateSlideRight() {
UIView.animateWithDuration(labelSlideDuration) {
UIView.animate(withDuration: labelSlideDuration) {
self.label.center.x += self.labelSlideLength
}
}
func animateToOriginalPosition() {
if self.label.center != self.labelOriginalCenter {
UIView.animateWithDuration(labelSlideDuration) {
UIView.animate(withDuration: labelSlideDuration) {
self.label.center = self.labelOriginalCenter
}
}
@ -439,14 +439,14 @@ extension GMStepper {
func animateLimitHitIfNeeded() {
if value == minimumValue {
animateLimitHitForButton(leftButton)
animateLimitHitForButton(button: leftButton)
} else if value == maximumValue {
animateLimitHitForButton(rightButton)
animateLimitHitForButton(button: rightButton)
}
}
func animateLimitHitForButton(button: UIButton){
UIView.animateWithDuration(limitHitAnimationDuration) {
UIView.animate(withDuration: limitHitAnimationDuration) {
button.backgroundColor = self.limitHitAnimationColor
}
}
@ -454,7 +454,7 @@ extension GMStepper {
// MARK: Timer
extension GMStepper {
func handleTimerFire(timer: NSTimer) {
func handleTimerFire(timer: Timer) {
timerFireCount += 1
if timerFireCount % timerFireCountModulo == 0 {
@ -463,7 +463,7 @@ extension GMStepper {
}
func scheduleTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(timerInterval, target: self, selector: #selector(GMStepper.handleTimerFire), userInfo: nil, repeats: true)
timer = Timer.scheduledTimer(timeInterval: timerInterval, target: self, selector: #selector(GMStepper.handleTimerFire), userInfo: nil, repeats: true)
}
func resetTimer() {

View File

@ -171,14 +171,18 @@
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Gunay Mert Karadogan";
TargetAttributes = {
319C19311B4843EB005EFEE5 = {
CreatedOnToolsVersion = 6.4;
DevelopmentTeam = 289M6XEDV4;
LastSwiftMigration = 0800;
};
319C19461B4843EB005EFEE5 = {
CreatedOnToolsVersion = 6.4;
DevelopmentTeam = 289M6XEDV4;
LastSwiftMigration = 0800;
TestTargetID = 319C19311B4843EB005EFEE5;
};
};
@ -285,8 +289,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@ -330,8 +336,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@ -350,6 +358,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.4;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
VALIDATE_PRODUCT = YES;
};
name = Release;
@ -358,10 +367,12 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 289M6XEDV4;
INFOPLIST_FILE = GMStepperExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.gunaymert.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
@ -369,10 +380,12 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 289M6XEDV4;
INFOPLIST_FILE = GMStepperExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.gunaymert.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
@ -380,6 +393,7 @@
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
DEVELOPMENT_TEAM = 289M6XEDV4;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
@ -392,6 +406,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.gunaymert.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GMStepperExample.app/GMStepperExample";
};
name = Debug;
@ -400,6 +415,7 @@
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
DEVELOPMENT_TEAM = 289M6XEDV4;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
@ -408,6 +424,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.gunaymert.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GMStepperExample.app/GMStepperExample";
};
name = Release;

View File

@ -13,7 +13,7 @@ class ViewController: UIViewController {
@IBOutlet weak var stepper: GMStepper!
override func viewDidLoad() {
super.viewDidLoad()
stepper.addTarget(self, action: #selector(ViewController.stepperValueChanged), forControlEvents: .ValueChanged)
stepper.addTarget(self, action: #selector(ViewController.stepperValueChanged), for: .valueChanged)
}
func stepperValueChanged(stepper: GMStepper) {