From 5b4f122b7be5a8b6ead4b5c989e38c8e57c11c21 Mon Sep 17 00:00:00 2001 From: Ben Vogelzang Date: Sat, 10 Dec 2016 12:36:06 -0600 Subject: [PATCH] update to Swift 3 and Xcode 8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit used Xcode’s built in swift migrator for this --- SevenSwitch.swift | 200 +++++++++--------- .../project.pbxproj | 7 + .../SevenSwitchExample/AppDelegate.swift | 14 +- .../SevenSwitchExample/ViewController.swift | 24 +-- 4 files changed, 126 insertions(+), 119 deletions(-) diff --git a/SevenSwitch.swift b/SevenSwitch.swift index c732318..e4d6f5b 100644 --- a/SevenSwitch.swift +++ b/SevenSwitch.swift @@ -25,14 +25,14 @@ import UIKit import QuartzCore -@IBDesignable @objc public class SevenSwitch: UIControl { +@IBDesignable @objc open class SevenSwitch: UIControl { // public /* * Set (without animation) whether the switch is on or off */ - @IBInspectable public var on: Bool { + @IBInspectable open var on: Bool { get { return switchValue } @@ -46,9 +46,9 @@ import QuartzCore * Sets the background color that shows when the switch off and actively being touched. * Defaults to light gray. */ - @IBInspectable public var activeColor: UIColor = UIColor(red: 0.89, green: 0.89, blue: 0.89, alpha: 1) { + @IBInspectable open var activeColor: UIColor = UIColor(red: 0.89, green: 0.89, blue: 0.89, alpha: 1) { willSet { - if self.on && !self.tracking { + if self.on && !self.isTracking { backgroundView.backgroundColor = newValue } } @@ -58,9 +58,9 @@ import QuartzCore * Sets the background color when the switch is off. * Defaults to clear color. */ - @IBInspectable public var inactiveColor: UIColor = UIColor.clearColor() { + @IBInspectable open var inactiveColor: UIColor = UIColor.clear { willSet { - if !self.on && !self.tracking { + if !self.on && !self.isTracking { backgroundView.backgroundColor = newValue } } @@ -70,11 +70,11 @@ import QuartzCore * Sets the background color that shows when the switch is on. * Defaults to green. */ - @IBInspectable public var onTintColor: UIColor = UIColor(red: 0.3, green: 0.85, blue: 0.39, alpha: 1) { + @IBInspectable open var onTintColor: UIColor = UIColor(red: 0.3, green: 0.85, blue: 0.39, alpha: 1) { willSet { - if self.on && !self.tracking { + if self.on && !self.isTracking { backgroundView.backgroundColor = newValue - backgroundView.layer.borderColor = newValue.CGColor + backgroundView.layer.borderColor = newValue.cgColor } } } @@ -82,10 +82,10 @@ import QuartzCore /* * Sets the border color that shows when the switch is off. Defaults to light gray. */ - @IBInspectable public var borderColor: UIColor = UIColor(red: 0.78, green: 0.78, blue: 0.8, alpha: 1) { + @IBInspectable open var borderColor: UIColor = UIColor(red: 0.78, green: 0.78, blue: 0.8, alpha: 1) { willSet { if !self.on { - backgroundView.layer.borderColor = newValue.CGColor + backgroundView.layer.borderColor = newValue.cgColor } } } @@ -93,12 +93,12 @@ import QuartzCore /* * Sets the knob color. Defaults to white. */ - @IBInspectable public var thumbTintColor: UIColor = UIColor.whiteColor() { + @IBInspectable open var thumbTintColor: UIColor = UIColor.white { willSet { if !userDidSpecifyOnThumbTintColor { onThumbTintColor = newValue } - if (!userDidSpecifyOnThumbTintColor || !self.on) && !self.tracking { + if (!userDidSpecifyOnThumbTintColor || !self.on) && !self.isTracking { thumbView.backgroundColor = newValue } } @@ -107,10 +107,10 @@ import QuartzCore /* * Sets the knob color that shows when the switch is on. Defaults to white. */ - @IBInspectable public var onThumbTintColor: UIColor = UIColor.whiteColor() { + @IBInspectable open var onThumbTintColor: UIColor = UIColor.white { willSet { userDidSpecifyOnThumbTintColor = true - if self.on && !self.tracking { + if self.on && !self.isTracking { thumbView.backgroundColor = newValue } } @@ -119,9 +119,9 @@ import QuartzCore /* * Sets the shadow color of the knob. Defaults to gray. */ - @IBInspectable public var shadowColor: UIColor = UIColor.grayColor() { + @IBInspectable open var shadowColor: UIColor = UIColor.gray { willSet { - thumbView.layer.shadowColor = newValue.CGColor + thumbView.layer.shadowColor = newValue.cgColor } } @@ -130,7 +130,7 @@ import QuartzCore * Set to NO to get a stylish square switch. * Defaults to YES. */ - @IBInspectable public var isRounded: Bool = true { + @IBInspectable open var isRounded: Bool = true { willSet { if newValue { backgroundView.layer.cornerRadius = self.frame.size.height * 0.5 @@ -141,14 +141,14 @@ import QuartzCore thumbView.layer.cornerRadius = 2 } - thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).CGPath + thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath } } /* * Sets the image that shows on the switch thumb. */ - @IBInspectable public var thumbImage: UIImage! { + @IBInspectable open var thumbImage: UIImage! { willSet { thumbImageView.image = newValue } @@ -159,7 +159,7 @@ import QuartzCore * The image is centered in the area not covered by the knob. * Make sure to size your images appropriately. */ - @IBInspectable public var onImage: UIImage! { + @IBInspectable open var onImage: UIImage! { willSet { onImageView.image = newValue } @@ -170,7 +170,7 @@ import QuartzCore * The image is centered in the area not covered by the knob. * Make sure to size your images appropriately. */ - @IBInspectable public var offImage: UIImage! { + @IBInspectable open var offImage: UIImage! { willSet { offImageView.image = newValue } @@ -180,13 +180,13 @@ import QuartzCore * Sets the text that shows when the switch is on. * The text is centered in the area not covered by the knob. */ - public var onLabel: UILabel! + open var onLabel: UILabel! /* * Sets the text that shows when the switch is off. * The text is centered in the area not covered by the knob. */ - public var offLabel: UILabel! + open var offLabel: UILabel! // internal internal var backgroundView: UIView! @@ -195,18 +195,18 @@ import QuartzCore internal var offImageView: UIImageView! internal var thumbImageView: UIImageView! // private - private var currentVisualValue: Bool = false - private var startTrackingValue: Bool = false - private var didChangeWhileTracking: Bool = false - private var isAnimating: Bool = false - private var userDidSpecifyOnThumbTintColor: Bool = false - private var switchValue: Bool = false + fileprivate var currentVisualValue: Bool = false + fileprivate var startTrackingValue: Bool = false + fileprivate var didChangeWhileTracking: Bool = false + fileprivate var isAnimating: Bool = false + fileprivate var userDidSpecifyOnThumbTintColor: Bool = false + fileprivate var switchValue: Bool = false /* * Initialization */ public convenience init() { - self.init(frame: CGRectMake(0, 0, 50, 30)) + self.init(frame: CGRect(x: 0, y: 0, width: 50, height: 30)) } required public init?(coder aDecoder: NSCoder) { @@ -216,7 +216,7 @@ import QuartzCore } override public init(frame: CGRect) { - let initialFrame = CGRectIsEmpty(frame) ? CGRectMake(0, 0, 50, 30) : frame + let initialFrame = frame.isEmpty ? CGRect(x: 0, y: 0, width: 50, height: 30) : frame super.init(frame: initialFrame) self.setup() @@ -226,66 +226,66 @@ import QuartzCore /* * Setup the individual elements of the switch and set default values */ - private func setup() { + fileprivate func setup() { // background - self.backgroundView = UIView(frame: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)) - backgroundView.backgroundColor = UIColor.clearColor() + self.backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) + backgroundView.backgroundColor = UIColor.clear backgroundView.layer.cornerRadius = self.frame.size.height * 0.5 - backgroundView.layer.borderColor = self.borderColor.CGColor + backgroundView.layer.borderColor = self.borderColor.cgColor backgroundView.layer.borderWidth = 1.0 - backgroundView.userInteractionEnabled = false + backgroundView.isUserInteractionEnabled = false backgroundView.clipsToBounds = true self.addSubview(backgroundView) // on/off images - self.onImageView = UIImageView(frame: CGRectMake(0, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height)) + self.onImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width - self.frame.size.height, height: self.frame.size.height)) onImageView.alpha = 1.0 - onImageView.contentMode = UIViewContentMode.Center + onImageView.contentMode = UIViewContentMode.center backgroundView.addSubview(onImageView) - self.offImageView = UIImageView(frame: CGRectMake(self.frame.size.height, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height)) + self.offImageView = UIImageView(frame: CGRect(x: self.frame.size.height, y: 0, width: self.frame.size.width - self.frame.size.height, height: self.frame.size.height)) offImageView.alpha = 1.0 - offImageView.contentMode = UIViewContentMode.Center + offImageView.contentMode = UIViewContentMode.center backgroundView.addSubview(offImageView) // labels - self.onLabel = UILabel(frame: CGRectMake(0, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height)) - onLabel.textAlignment = NSTextAlignment.Center - onLabel.textColor = UIColor.lightGrayColor() - onLabel.font = UIFont.systemFontOfSize(12) + self.onLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width - self.frame.size.height, height: self.frame.size.height)) + onLabel.textAlignment = NSTextAlignment.center + onLabel.textColor = UIColor.lightGray + onLabel.font = UIFont.systemFont(ofSize: 12) backgroundView.addSubview(onLabel) - self.offLabel = UILabel(frame: CGRectMake(self.frame.size.height, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height)) - offLabel.textAlignment = NSTextAlignment.Center - offLabel.textColor = UIColor.lightGrayColor() - offLabel.font = UIFont.systemFontOfSize(12) + self.offLabel = UILabel(frame: CGRect(x: self.frame.size.height, y: 0, width: self.frame.size.width - self.frame.size.height, height: self.frame.size.height)) + offLabel.textAlignment = NSTextAlignment.center + offLabel.textColor = UIColor.lightGray + offLabel.font = UIFont.systemFont(ofSize: 12) backgroundView.addSubview(offLabel) // thumb - self.thumbView = UIView(frame: CGRectMake(1, 1, self.frame.size.height - 2, self.frame.size.height - 2)) + self.thumbView = UIView(frame: CGRect(x: 1, y: 1, width: self.frame.size.height - 2, height: self.frame.size.height - 2)) thumbView.backgroundColor = self.thumbTintColor thumbView.layer.cornerRadius = (self.frame.size.height * 0.5) - 1 - thumbView.layer.shadowColor = self.shadowColor.CGColor + thumbView.layer.shadowColor = self.shadowColor.cgColor thumbView.layer.shadowRadius = 2.0 thumbView.layer.shadowOpacity = 0.5 - thumbView.layer.shadowOffset = CGSizeMake(0, 3) - thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).CGPath + thumbView.layer.shadowOffset = CGSize(width: 0, height: 3) + thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath thumbView.layer.masksToBounds = false - thumbView.userInteractionEnabled = false + thumbView.isUserInteractionEnabled = false self.addSubview(thumbView) // thumb image - self.thumbImageView = UIImageView(frame: CGRectMake(0, 0, thumbView.frame.size.width, thumbView.frame.size.height)) - thumbImageView.contentMode = UIViewContentMode.Center - thumbImageView.autoresizingMask = UIViewAutoresizing.FlexibleWidth + self.thumbImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: thumbView.frame.size.width, height: thumbView.frame.size.height)) + thumbImageView.contentMode = UIViewContentMode.center + thumbImageView.autoresizingMask = UIViewAutoresizing.flexibleWidth thumbView.addSubview(thumbImageView) self.on = false } - override public func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool { - super.beginTrackingWithTouch(touch, withEvent: event) + override open func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { + super.beginTracking(touch, with: event) startTrackingValue = self.on didChangeWhileTracking = false @@ -293,14 +293,14 @@ import QuartzCore let activeKnobWidth = self.bounds.size.height - 2 + 5 isAnimating = true - UIView.animateWithDuration(0.3, delay: 0.0, options: [UIViewAnimationOptions.CurveEaseOut, UIViewAnimationOptions.BeginFromCurrentState], animations: { + UIView.animate(withDuration: 0.3, delay: 0.0, options: [UIViewAnimationOptions.curveEaseOut, UIViewAnimationOptions.beginFromCurrentState], animations: { if self.on { - self.thumbView.frame = CGRectMake(self.bounds.size.width - (activeKnobWidth + 1), self.thumbView.frame.origin.y, activeKnobWidth, self.thumbView.frame.size.height) + self.thumbView.frame = CGRect(x: self.bounds.size.width - (activeKnobWidth + 1), y: self.thumbView.frame.origin.y, width: activeKnobWidth, height: self.thumbView.frame.size.height) self.backgroundView.backgroundColor = self.onTintColor self.thumbView.backgroundColor = self.onThumbTintColor } else { - self.thumbView.frame = CGRectMake(self.thumbView.frame.origin.x, self.thumbView.frame.origin.y, activeKnobWidth, self.thumbView.frame.size.height) + self.thumbView.frame = CGRect(x: self.thumbView.frame.origin.x, y: self.thumbView.frame.origin.y, width: activeKnobWidth, height: self.thumbView.frame.size.height) self.backgroundView.backgroundColor = self.activeColor self.thumbView.backgroundColor = self.thumbTintColor } @@ -311,11 +311,11 @@ import QuartzCore return true } - override public func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool { - super.continueTrackingWithTouch(touch, withEvent: event) + override open func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { + super.continueTracking(touch, with: event) // Get touch location - let lastPoint = touch.locationInView(self) + let lastPoint = touch.location(in: self) // update the switch to the correct visuals depending on if // they moved their touch to the right or left side of the switch @@ -335,8 +335,8 @@ import QuartzCore return true } - override public func endTrackingWithTouch(touch: UITouch?, withEvent event: UIEvent?) { - super.endTrackingWithTouch(touch, withEvent: event) + override open func endTracking(_ touch: UITouch?, with event: UIEvent?) { + super.endTracking(touch, with: event) let previousValue = self.on @@ -348,12 +348,12 @@ import QuartzCore } if previousValue != self.on { - self.sendActionsForControlEvents(UIControlEvents.ValueChanged) + self.sendActions(for: UIControlEvents.valueChanged) } } - override public func cancelTrackingWithEvent(event: UIEvent?) { - super.cancelTrackingWithEvent(event) + override open func cancelTracking(with event: UIEvent?) { + super.cancelTracking(with: event) // just animate back to the original value if self.on { @@ -364,29 +364,29 @@ import QuartzCore } } - override public func layoutSubviews() { + override open func layoutSubviews() { super.layoutSubviews() if !isAnimating { let frame = self.frame // background - backgroundView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height) + backgroundView.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height) backgroundView.layer.cornerRadius = self.isRounded ? frame.size.height * 0.5 : 2 // images - onImageView.frame = CGRectMake(0, 0, frame.size.width - frame.size.height, frame.size.height) - offImageView.frame = CGRectMake(frame.size.height, 0, frame.size.width - frame.size.height, frame.size.height) - self.onLabel.frame = CGRectMake(0, 0, frame.size.width - frame.size.height, frame.size.height) - self.offLabel.frame = CGRectMake(frame.size.height, 0, frame.size.width - frame.size.height, frame.size.height) + onImageView.frame = CGRect(x: 0, y: 0, width: frame.size.width - frame.size.height, height: frame.size.height) + offImageView.frame = CGRect(x: frame.size.height, y: 0, width: frame.size.width - frame.size.height, height: frame.size.height) + self.onLabel.frame = CGRect(x: 0, y: 0, width: frame.size.width - frame.size.height, height: frame.size.height) + self.offLabel.frame = CGRect(x: frame.size.height, y: 0, width: frame.size.width - frame.size.height, height: frame.size.height) // thumb let normalKnobWidth = frame.size.height - 2 if self.on { - thumbView.frame = CGRectMake(frame.size.width - (normalKnobWidth + 1), 1, frame.size.height - 2, normalKnobWidth) + thumbView.frame = CGRect(x: frame.size.width - (normalKnobWidth + 1), y: 1, width: frame.size.height - 2, height: normalKnobWidth) } else { - thumbView.frame = CGRectMake(1, 1, normalKnobWidth, normalKnobWidth) + thumbView.frame = CGRect(x: 1, y: 1, width: normalKnobWidth, height: normalKnobWidth) } thumbView.layer.cornerRadius = self.isRounded ? (frame.size.height * 0.5) - 1 : 2 @@ -396,7 +396,7 @@ import QuartzCore /* * Set the state of the switch to on or off, optionally animating the transition. */ - public func setOn(isOn: Bool, animated: Bool) { + open func setOn(_ isOn: Bool, animated: Bool) { switchValue = isOn if on { @@ -412,7 +412,7 @@ import QuartzCore * * @return BOOL YES if switch is on. NO if switch is off */ - public func isOn() -> Bool { + open func isOn() -> Bool { return self.on } @@ -420,21 +420,21 @@ import QuartzCore * update the looks of the switch to be in the on position * optionally make it animated */ - private func showOn(animated: Bool) { + fileprivate func showOn(_ animated: Bool) { let normalKnobWidth = self.bounds.size.height - 2 let activeKnobWidth = normalKnobWidth + 5 if animated { isAnimating = true - UIView.animateWithDuration(0.3, delay: 0.0, options: [UIViewAnimationOptions.CurveEaseOut, UIViewAnimationOptions.BeginFromCurrentState], animations: { - if self.tracking { - self.thumbView.frame = CGRectMake(self.bounds.size.width - (activeKnobWidth + 1), self.thumbView.frame.origin.y, activeKnobWidth, self.thumbView.frame.size.height) + UIView.animate(withDuration: 0.3, delay: 0.0, options: [UIViewAnimationOptions.curveEaseOut, UIViewAnimationOptions.beginFromCurrentState], animations: { + if self.isTracking { + self.thumbView.frame = CGRect(x: self.bounds.size.width - (activeKnobWidth + 1), y: self.thumbView.frame.origin.y, width: activeKnobWidth, height: self.thumbView.frame.size.height) } else { - self.thumbView.frame = CGRectMake(self.bounds.size.width - (normalKnobWidth + 1), self.thumbView.frame.origin.y, normalKnobWidth, self.thumbView.frame.size.height) + self.thumbView.frame = CGRect(x: self.bounds.size.width - (normalKnobWidth + 1), y: self.thumbView.frame.origin.y, width: normalKnobWidth, height: self.thumbView.frame.size.height) } self.backgroundView.backgroundColor = self.onTintColor - self.backgroundView.layer.borderColor = self.onTintColor.CGColor + self.backgroundView.layer.borderColor = self.onTintColor.cgColor self.thumbView.backgroundColor = self.onThumbTintColor self.onImageView.alpha = 1.0 self.offImageView.alpha = 0 @@ -445,15 +445,15 @@ import QuartzCore }) } else { - if self.tracking { - thumbView.frame = CGRectMake(self.bounds.size.width - (activeKnobWidth + 1), thumbView.frame.origin.y, activeKnobWidth, thumbView.frame.size.height) + if self.isTracking { + thumbView.frame = CGRect(x: self.bounds.size.width - (activeKnobWidth + 1), y: thumbView.frame.origin.y, width: activeKnobWidth, height: thumbView.frame.size.height) } else { - thumbView.frame = CGRectMake(self.bounds.size.width - (normalKnobWidth + 1), thumbView.frame.origin.y, normalKnobWidth, thumbView.frame.size.height) + thumbView.frame = CGRect(x: self.bounds.size.width - (normalKnobWidth + 1), y: thumbView.frame.origin.y, width: normalKnobWidth, height: thumbView.frame.size.height) } backgroundView.backgroundColor = self.onTintColor - backgroundView.layer.borderColor = self.onTintColor.CGColor + backgroundView.layer.borderColor = self.onTintColor.cgColor thumbView.backgroundColor = self.onThumbTintColor onImageView.alpha = 1.0 offImageView.alpha = 0 @@ -468,23 +468,23 @@ import QuartzCore * update the looks of the switch to be in the off position * optionally make it animated */ - private func showOff(animated: Bool) { + fileprivate func showOff(_ animated: Bool) { let normalKnobWidth = self.bounds.size.height - 2 let activeKnobWidth = normalKnobWidth + 5 if animated { isAnimating = true - UIView.animateWithDuration(0.3, delay: 0.0, options: [UIViewAnimationOptions.CurveEaseOut, UIViewAnimationOptions.BeginFromCurrentState], animations: { - if self.tracking { - self.thumbView.frame = CGRectMake(1, self.thumbView.frame.origin.y, activeKnobWidth, self.thumbView.frame.size.height); + UIView.animate(withDuration: 0.3, delay: 0.0, options: [UIViewAnimationOptions.curveEaseOut, UIViewAnimationOptions.beginFromCurrentState], animations: { + if self.isTracking { + self.thumbView.frame = CGRect(x: 1, y: self.thumbView.frame.origin.y, width: activeKnobWidth, height: self.thumbView.frame.size.height); self.backgroundView.backgroundColor = self.activeColor } else { - self.thumbView.frame = CGRectMake(1, self.thumbView.frame.origin.y, normalKnobWidth, self.thumbView.frame.size.height); + self.thumbView.frame = CGRect(x: 1, y: self.thumbView.frame.origin.y, width: normalKnobWidth, height: self.thumbView.frame.size.height); self.backgroundView.backgroundColor = self.inactiveColor } - self.backgroundView.layer.borderColor = self.borderColor.CGColor + self.backgroundView.layer.borderColor = self.borderColor.cgColor self.thumbView.backgroundColor = self.thumbTintColor self.onImageView.alpha = 0 self.offImageView.alpha = 1.0 @@ -496,15 +496,15 @@ import QuartzCore }) } else { - if (self.tracking) { - thumbView.frame = CGRectMake(1, thumbView.frame.origin.y, activeKnobWidth, thumbView.frame.size.height) + if (self.isTracking) { + thumbView.frame = CGRect(x: 1, y: thumbView.frame.origin.y, width: activeKnobWidth, height: thumbView.frame.size.height) backgroundView.backgroundColor = self.activeColor } else { - thumbView.frame = CGRectMake(1, thumbView.frame.origin.y, normalKnobWidth, thumbView.frame.size.height) + thumbView.frame = CGRect(x: 1, y: thumbView.frame.origin.y, width: normalKnobWidth, height: thumbView.frame.size.height) backgroundView.backgroundColor = self.inactiveColor } - backgroundView.layer.borderColor = self.borderColor.CGColor + backgroundView.layer.borderColor = self.borderColor.cgColor thumbView.backgroundColor = self.thumbTintColor onImageView.alpha = 0 offImageView.alpha = 1.0 diff --git a/SevenSwitchExample/SevenSwitchExample.xcodeproj/project.pbxproj b/SevenSwitchExample/SevenSwitchExample.xcodeproj/project.pbxproj index 4d323ce..84dde94 100644 --- a/SevenSwitchExample/SevenSwitchExample.xcodeproj/project.pbxproj +++ b/SevenSwitchExample/SevenSwitchExample.xcodeproj/project.pbxproj @@ -147,6 +147,11 @@ LastSwiftUpdateCheck = 0700; LastUpgradeCheck = 0700; ORGANIZATIONNAME = "Ben Vogelzang"; + TargetAttributes = { + 45F7C62D176D202700AE1301 = { + LastSwiftMigration = 0810; + }; + }; }; buildConfigurationList = 45F7C629176D202700AE1301 /* Build configuration list for PBXProject "SevenSwitchExample" */; compatibilityVersion = "Xcode 3.2"; @@ -288,6 +293,7 @@ SWIFT_INSTALL_OBJC_HEADER = NO; SWIFT_OBJC_BRIDGING_HEADER = ""; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; WRAPPER_EXTENSION = app; }; name = Debug; @@ -305,6 +311,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_INSTALL_OBJC_HEADER = NO; SWIFT_OBJC_BRIDGING_HEADER = ""; + SWIFT_VERSION = 3.0; WRAPPER_EXTENSION = app; }; name = Release; diff --git a/SevenSwitchExample/SevenSwitchExample/AppDelegate.swift b/SevenSwitchExample/SevenSwitchExample/AppDelegate.swift index a164d1d..20ec756 100644 --- a/SevenSwitchExample/SevenSwitchExample/AppDelegate.swift +++ b/SevenSwitchExample/SevenSwitchExample/AppDelegate.swift @@ -14,32 +14,32 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } -} \ No newline at end of file +} diff --git a/SevenSwitchExample/SevenSwitchExample/ViewController.swift b/SevenSwitchExample/SevenSwitchExample/ViewController.swift index a9ed8a9..f8c23f0 100644 --- a/SevenSwitchExample/SevenSwitchExample/ViewController.swift +++ b/SevenSwitchExample/SevenSwitchExample/ViewController.swift @@ -19,18 +19,18 @@ class ViewController: UIViewController { // this will create the switch with default dimensions, you'll still need to set the position though // you also have the option to pass in a frame of any size you choose - let mySwitch = SevenSwitch(frame: CGRectZero) - mySwitch.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5) - mySwitch.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged) + let mySwitch = SevenSwitch(frame: CGRect.zero) + mySwitch.center = CGPoint(x: self.view.bounds.size.width * 0.5, y: self.view.bounds.size.height * 0.5) + mySwitch.addTarget(self, action: #selector(ViewController.switchChanged(_:)), for: UIControlEvents.valueChanged) self.view.addSubview(mySwitch) // turn the switch on mySwitch.on = true // Example of a bigger switch with images - let mySwitch2 = SevenSwitch(frame: CGRectMake(0, 0, 100, 50)) - mySwitch2.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5 - 80) - mySwitch2.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged) + let mySwitch2 = SevenSwitch(frame: CGRect(x: 0, y: 0, width: 100, height: 50)) + mySwitch2.center = CGPoint(x: self.view.bounds.size.width * 0.5, y: self.view.bounds.size.height * 0.5 - 80) + mySwitch2.addTarget(self, action: #selector(ViewController.switchChanged(_:)), for: UIControlEvents.valueChanged) mySwitch2.offImage = UIImage(named: "cross.png") mySwitch2.onImage = UIImage(named: "check.png") mySwitch2.onTintColor = UIColor(hue: 0.08, saturation: 0.74, brightness: 1.00, alpha: 1.00) @@ -41,9 +41,9 @@ class ViewController: UIViewController { mySwitch2.setOn(true, animated: true) // Example of color customization - let mySwitch3 = SevenSwitch(frame: CGRectZero) - mySwitch3.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5 + 70) - mySwitch3.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged) + let mySwitch3 = SevenSwitch(frame: CGRect.zero) + mySwitch3.center = CGPoint(x: self.view.bounds.size.width * 0.5, y: self.view.bounds.size.height * 0.5 + 70) + mySwitch3.addTarget(self, action: #selector(ViewController.switchChanged(_:)), for: UIControlEvents.valueChanged) self.view.addSubview(mySwitch3) //self.view.backgroundColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f]; @@ -51,11 +51,11 @@ class ViewController: UIViewController { mySwitch3.activeColor = UIColor(red: 0.07, green: 0.09, blue: 0.11, alpha: 1) mySwitch3.inactiveColor = UIColor(red: 0.07, green: 0.09, blue: 0.11, alpha: 1) mySwitch3.onTintColor = UIColor(red: 0.45, green: 0.58, blue: 0.67, alpha: 1) - mySwitch3.borderColor = UIColor.clearColor() - mySwitch3.shadowColor = UIColor.blackColor() + mySwitch3.borderColor = UIColor.clear + mySwitch3.shadowColor = UIColor.black } - func switchChanged(sender: SevenSwitch) { + func switchChanged(_ sender: SevenSwitch) { print("Changed value to: \(sender.on)") } }