Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions ButtonBackgroundColor-iOS/UIButton+ButtonBackgroundColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -72,7 +73,7 @@ public extension UIButton

- parameter sender: the button.
*/
func buttonTouchUpInside(sender: UIButton)
public func buttonTouchUpInside(sender: UIButton)
{
sender.backgroundColor = normalBackgroundColor()
}
Expand All @@ -82,7 +83,7 @@ public extension UIButton

- parameter sender: the button.
*/
func buttonTouchUpOutside(sender: UIButton)
public func buttonTouchUpOutside(sender: UIButton)
{
sender.backgroundColor = normalBackgroundColor()
}
Expand All @@ -92,7 +93,7 @@ public extension UIButton

- parameter sender: the button.
*/
func buttonTouchDown(sender: UIButton)
public func buttonTouchDown(sender: UIButton)
{
sender.backgroundColor = highlightedBackgroundColor()
}
Expand All @@ -102,8 +103,8 @@ public extension UIButton

- parameter sender: the button.
*/
func buttonTouchCancel(sender: UIButton)
public func buttonTouchCancel(sender: UIButton)
{
sender.backgroundColor = normalBackgroundColor()
}
}
}
17 changes: 16 additions & 1 deletion ButtonBackgroundColor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -345,21 +353,25 @@
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;
};
C0D133001CBD36B200CF0C2B /* Release */ = {
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;
};
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -415,6 +429,7 @@
C0D133161CBD555700CF0C2B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
15 changes: 8 additions & 7 deletions ButtonBackgroundColor/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,31 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> 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:.
}

Expand Down
15 changes: 9 additions & 6 deletions ButtonBackgroundColor/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
24 changes: 12 additions & 12 deletions ButtonBackgroundColorTests/ButtonBackgroundColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand Down