From 2592430e6691ffb2e5a2291de7a8739b3d91515d Mon Sep 17 00:00:00 2001 From: Michael Gray Date: Thu, 22 Sep 2016 15:53:34 -0700 Subject: [PATCH] ported to swift 3 --- .../UIButton+ButtonBackgroundColor.swift | 21 ++++++++-------- .../project.pbxproj | 17 ++++++++++++- .../xcschemes/ButtonBackgroundColor.xcscheme | 2 +- .../ButtonBackgroundColorTests.xcscheme | 2 +- .../Example/AppDelegate.swift | 15 ++++++------ .../Example/ViewController.swift | 15 +++++++----- .../ButtonBackgroundColorTests.swift | 24 +++++++++---------- 7 files changed, 58 insertions(+), 38 deletions(-) diff --git a/ButtonBackgroundColor-iOS/UIButton+ButtonBackgroundColor.swift b/ButtonBackgroundColor-iOS/UIButton+ButtonBackgroundColor.swift index edc97e3..51da54e 100644 --- a/ButtonBackgroundColor-iOS/UIButton+ButtonBackgroundColor.swift +++ b/ButtonBackgroundColor-iOS/UIButton+ButtonBackgroundColor.swift @@ -27,16 +27,17 @@ public extension UIButton - parameter highlighted: the color for a highlighted state. */ @objc(bbc_backgroundColorNormal:backgroundColorHighlighted:) - public func backgroundColorForStates(normal normal: UIColor, highlighted: UIColor) + public func backgroundColorForStates(normal: UIColor, highlighted: UIColor) { //set normal background color backgroundColor = normal //handle states - addTarget(self, action:#selector(UIButton.buttonTouchUpInside(_:)), forControlEvents:.TouchUpInside) - addTarget(self, action:#selector(UIButton.buttonTouchUpOutside(_:)), forControlEvents:.TouchUpOutside) - addTarget(self, action:#selector(UIButton.buttonTouchDown(_:)), forControlEvents:.TouchDown) - addTarget(self, action:#selector(UIButton.buttonTouchCancel(_:)), forControlEvents:.TouchCancel) + + addTarget(self, action:#selector(UIButton.buttonTouchUpInside), for:.touchUpInside) + addTarget(self, action:#selector(UIButton.buttonTouchUpOutside), for:.touchUpOutside) + addTarget(self, action:#selector(UIButton.buttonTouchDown), for:.touchDown) + addTarget(self, action:#selector(UIButton.buttonTouchCancel), for:.touchCancel) //store colors objc_setAssociatedObject(self, &BackgroundColorNormal, normal, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) @@ -72,7 +73,7 @@ public extension UIButton - parameter sender: the button. */ - func buttonTouchUpInside(sender: UIButton) + public func buttonTouchUpInside(sender: UIButton) { sender.backgroundColor = normalBackgroundColor() } @@ -82,7 +83,7 @@ public extension UIButton - parameter sender: the button. */ - func buttonTouchUpOutside(sender: UIButton) + public func buttonTouchUpOutside(sender: UIButton) { sender.backgroundColor = normalBackgroundColor() } @@ -92,7 +93,7 @@ public extension UIButton - parameter sender: the button. */ - func buttonTouchDown(sender: UIButton) + public func buttonTouchDown(sender: UIButton) { sender.backgroundColor = highlightedBackgroundColor() } @@ -102,8 +103,8 @@ public extension UIButton - parameter sender: the button. */ - func buttonTouchCancel(sender: UIButton) + public func buttonTouchCancel(sender: UIButton) { sender.backgroundColor = normalBackgroundColor() } -} \ No newline at end of file +} diff --git a/ButtonBackgroundColor.xcodeproj/project.pbxproj b/ButtonBackgroundColor.xcodeproj/project.pbxproj index 9452c05..4c90ac7 100644 --- a/ButtonBackgroundColor.xcodeproj/project.pbxproj +++ b/ButtonBackgroundColor.xcodeproj/project.pbxproj @@ -160,14 +160,17 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = GabrielMassana; TargetAttributes = { C0D132EB1CBD36B200CF0C2B = { CreatedOnToolsVersion = 7.2; + DevelopmentTeam = A6GQ5E3LKE; + LastSwiftMigration = 0800; }; C0D1330C1CBD555700CF0C2B = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; TestTargetID = C0D132EB1CBD36B200CF0C2B; }; }; @@ -273,8 +276,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"; @@ -317,8 +322,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"; @@ -337,6 +344,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.2; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; VALIDATE_PRODUCT = YES; }; name = Release; @@ -345,10 +353,12 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = A6GQ5E3LKE; INFOPLIST_FILE = ButtonBackgroundColor/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "Gabriel-Massana.ButtonBackgroundColor"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -356,10 +366,12 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = A6GQ5E3LKE; INFOPLIST_FILE = ButtonBackgroundColor/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "Gabriel-Massana.ButtonBackgroundColor"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -371,6 +383,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "Gabriel-Massana.ButtonBackgroundColorTests"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ButtonBackgroundColor.app/ButtonBackgroundColor"; }; name = Debug; @@ -383,6 +396,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "Gabriel-Massana.ButtonBackgroundColorTests"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ButtonBackgroundColor.app/ButtonBackgroundColor"; }; name = Release; @@ -415,6 +429,7 @@ C0D133161CBD555700CF0C2B /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/ButtonBackgroundColor.xcodeproj/xcshareddata/xcschemes/ButtonBackgroundColor.xcscheme b/ButtonBackgroundColor.xcodeproj/xcshareddata/xcschemes/ButtonBackgroundColor.xcscheme index 67875db..6322928 100644 --- a/ButtonBackgroundColor.xcodeproj/xcshareddata/xcschemes/ButtonBackgroundColor.xcscheme +++ b/ButtonBackgroundColor.xcodeproj/xcshareddata/xcschemes/ButtonBackgroundColor.xcscheme @@ -1,6 +1,6 @@ Bool { - // Override point for customization after application launch. + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> 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:. } diff --git a/ButtonBackgroundColor/Example/ViewController.swift b/ButtonBackgroundColor/Example/ViewController.swift index 84d24ea..98fde65 100644 --- a/ButtonBackgroundColor/Example/ViewController.swift +++ b/ButtonBackgroundColor/Example/ViewController.swift @@ -14,17 +14,20 @@ class ViewController: UIViewController { super.viewDidLoad() - let button: UIButton = UIButton(type: .Custom) + let button: UIButton = UIButton(type: .custom) - button.frame = CGRectMake(0.0, 100.0, CGRectGetWidth(UIScreen.mainScreen().bounds), 80.0) - button.backgroundColorForStates(normal: UIColor.redColor(), highlighted: UIColor.blueColor()) + let mainscreenWidth = UIScreen.main.bounds.width + + button.frame = CGRect(x: 0.0, y: 100.0, width: mainscreenWidth, height: 80.0) + + button.backgroundColorForStates(normal: UIColor.red, highlighted: UIColor.blue) view.addSubview(button) - let buttonWithout: UIButton = UIButton(type: .Custom) + let buttonWithout: UIButton = UIButton(type: .custom) - buttonWithout.frame = CGRectMake(0.0, 200.0, CGRectGetWidth(UIScreen.mainScreen().bounds), 80.0) - buttonWithout.backgroundColor = UIColor.greenColor() + buttonWithout.frame = CGRect(x: 0.0, y: 200.0, width: mainscreenWidth, height: 80.0) + buttonWithout.backgroundColor = UIColor.green view.addSubview(buttonWithout) } diff --git a/ButtonBackgroundColorTests/ButtonBackgroundColorTests.swift b/ButtonBackgroundColorTests/ButtonBackgroundColorTests.swift index a4f567a..5db804f 100644 --- a/ButtonBackgroundColorTests/ButtonBackgroundColorTests.swift +++ b/ButtonBackgroundColorTests/ButtonBackgroundColorTests.swift @@ -26,14 +26,14 @@ class ButtonBackgroundColorTests: XCTestCase { { super.setUp() - normalColor = UIColor.redColor() - highlightedColor = UIColor.orangeColor() + normalColor = UIColor.red + highlightedColor = UIColor.orange - button = UIButton(type: .Custom) + button = UIButton(type: .custom) button!.backgroundColorForStates(normal: normalColor!, highlighted: highlightedColor!) - buttonWithoutExtension = UIButton(type: .Custom) + buttonWithoutExtension = UIButton(type: .custom) buttonWithoutExtension!.backgroundColor = normalColor } @@ -87,28 +87,28 @@ class ButtonBackgroundColorTests: XCTestCase { func test_bbc_controlEventTouchDown() { - button!.sendActionsForControlEvents(.TouchDown) + button!.sendActions(for: .touchDown) XCTAssertEqual(button!.backgroundColor, highlightedColor, "fail_test_bbc_controlEventTouchDown"); } func test_bbc_controlEventTouchUpInside() { - button!.sendActionsForControlEvents(.TouchUpInside) + button!.sendActions(for: .touchUpInside) XCTAssertEqual(button!.backgroundColor, normalColor, "test_bbc_controlEventTouchUpInside"); } func test_bbc_controlEventTouchUpOutside() { - button!.sendActionsForControlEvents(.TouchUpOutside) + button!.sendActions(for: .touchUpOutside) XCTAssertEqual(button!.backgroundColor, normalColor, "test_bbc_controlEventTouchUpOutside"); } func test_bbc_controlEventTouchCancel() { - button!.sendActionsForControlEvents(.TouchCancel) + button!.sendActions(for: .touchCancel) XCTAssertEqual(button!.backgroundColor, normalColor, "test_bbc_controlEventTouchCancel"); } @@ -117,28 +117,28 @@ class ButtonBackgroundColorTests: XCTestCase { func test_bbc_controlEventTouchDown_withoutExtension() { - buttonWithoutExtension!.sendActionsForControlEvents(.TouchDown) + buttonWithoutExtension!.sendActions(for: .touchDown) XCTAssertEqual(button!.backgroundColor, normalColor, "test_bbc_controlEventTouchDown_withoutExtension"); } func test_bbc_controlEventTouchUpInside_withoutExtension() { - buttonWithoutExtension!.sendActionsForControlEvents(.TouchUpInside) + buttonWithoutExtension!.sendActions(for: .touchUpInside) XCTAssertEqual(button!.backgroundColor, normalColor, "test_bbc_controlEventTouchUpInside_withoutExtension"); } func test_bbc_controlEventTouchUpOutside_withoutExtension() { - buttonWithoutExtension!.sendActionsForControlEvents(.TouchUpOutside) + buttonWithoutExtension!.sendActions(for: .touchUpOutside) XCTAssertEqual(button!.backgroundColor, normalColor, "test_bbc_controlEventTouchUpOutside_withoutExtension"); } func test_bbc_controlEventTouchCancel_withoutExtension() { - buttonWithoutExtension!.sendActionsForControlEvents(.TouchCancel) + buttonWithoutExtension!.sendActions(for: .touchCancel) XCTAssertEqual(button!.backgroundColor, normalColor, "test_bbc_controlEventTouchCancel_withoutExtension"); }