Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Class to create flat buttons
  • Loading branch information
ijason committed Jun 24, 2013
1 parent 7fb3154 commit e589cfe
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 2 deletions.
35 changes: 35 additions & 0 deletions FlatButton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// FlatButton.h
// FlatButtons
//
// Created by JASON EVERETT on 6/22/13.
// Copyright (c) 2013 JASON EVERETT. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

#import <UIKit/UIKit.h>

@interface FlatButton : UIButton
@property(strong, nonatomic)UIColor *myColor;
-(id)initWithFrame:(CGRect)frame withBackgroundColor:(UIColor*)backgroundColor;
@end
111 changes: 111 additions & 0 deletions FlatButton.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// FlatButton.m
// FlatButtons
//
// Created by JASON EVERETT on 6/22/13.
// Copyright (c) 2013 JASON EVERETT. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

#import "FlatButton.h"
#import <QuartzCore/QuartzCore.h>

@interface FlatButton()
-(void)wasPressed;
-(void)endedPress;
-(void)makeFlat:(FlatButton*)button withBackgroundColor:(UIColor*)backgroundColor;
@end

@implementation FlatButton

- (id)initWithFrame:(CGRect)frame withBackgroundColor:(UIColor*)backgroundColor
{
self = [super initWithFrame:frame];
if (self) {
[self makeFlat:self withBackgroundColor:backgroundColor];
}
return self;
}

-(void)makeFlat:(FlatButton*)button withBackgroundColor:(UIColor*)backgroundColor
{
//save our color so we can alter it upon a touch event
self.myColor = backgroundColor;
[self setBackgroundColor:backgroundColor];
[self addTarget:self action:@selector(wasPressed) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(endedPress) forControlEvents:UIControlEventTouchUpInside];
}

//When button is touched, grab our existing color and make it 20% darker (or lighter if its black)
//We will return it to its original state when the touch is lifted and touchesEnded:withEvent: is called
-(void)wasPressed
{
UIColor *newColor;
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0, white = 0.0;

//Check if we're working with atleast iOS 5.0
if([self.myColor respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
[self.myColor getRed:&red green:&green blue:&blue alpha:&alpha];
[self.myColor getWhite:&white alpha:&alpha];

//test if we're working with a grayscale, black or RGB color
if(!(red + green + blue) && white){
//grayscale
newColor = [UIColor colorWithWhite:white - 0.2 alpha:alpha];
} else if(!(red + green + blue) && !white) {
//black
newColor = [UIColor colorWithWhite:white + 0.2 alpha:alpha];
} else{
//RGB
newColor = [UIColor colorWithRed:red - 0.2 green:green - 0.2 blue:blue - 0.2 alpha:alpha];
}
} else if(CGColorGetNumberOfComponents(self.myColor.CGColor) == 4) {
//for earlier than ios 5
const CGFloat *components = CGColorGetComponents(self.myColor.CGColor);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];

newColor = [UIColor colorWithRed:red - 0.2 green:green - 0.2 blue:blue - 0.2 alpha:alpha];
} else if(CGColorGetNumberOfComponents(self.myColor.CGColor) == 2){
//if we have a non-RGB color
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
[self.myColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];

newColor = [UIColor colorWithHue:hue - 0.2 saturation:saturation - 0.2 brightness:brightness - 0.2 alpha:alpha];
}

self.backgroundColor = newColor;

}

-(void)endedPress
{
//Reset our button to its original color
self.backgroundColor = self.myColor;
}

@end
6 changes: 6 additions & 0 deletions FlatButtons.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
9644A3811776966F0008255E /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 9644A3801776966F0008255E /* [email protected] */; };
9644A3841776966F0008255E /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9644A3821776966F0008255E /* MainStoryboard.storyboard */; };
9644A3871776966F0008255E /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9644A3861776966F0008255E /* ViewController.m */; };
9644A38F177699700008255E /* FlatButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 9644A38E177699700008255E /* FlatButton.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -37,6 +38,8 @@
9644A3831776966F0008255E /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/MainStoryboard.storyboard; sourceTree = "<group>"; };
9644A3851776966F0008255E /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
9644A3861776966F0008255E /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
9644A38D177699700008255E /* FlatButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FlatButton.h; sourceTree = "<group>"; };
9644A38E177699700008255E /* FlatButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FlatButton.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -89,6 +92,8 @@
9644A3851776966F0008255E /* ViewController.h */,
9644A3861776966F0008255E /* ViewController.m */,
9644A3711776966F0008255E /* Supporting Files */,
9644A38D177699700008255E /* FlatButton.h */,
9644A38E177699700008255E /* FlatButton.m */,
);
path = FlatButtons;
sourceTree = "<group>";
Expand Down Expand Up @@ -176,6 +181,7 @@
9644A3771776966F0008255E /* main.m in Sources */,
9644A37B1776966F0008255E /* AppDelegate.m in Sources */,
9644A3871776966F0008255E /* ViewController.m in Sources */,
9644A38F177699700008255E /* FlatButton.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0460"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9644A3661776966F0008255E"
BuildableName = "FlatButtons.app"
BlueprintName = "FlatButtons"
ReferencedContainer = "container:FlatButtons.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9644A3661776966F0008255E"
BuildableName = "FlatButtons.app"
BlueprintName = "FlatButtons"
ReferencedContainer = "container:FlatButtons.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9644A3661776966F0008255E"
BuildableName = "FlatButtons.app"
BlueprintName = "FlatButtons"
ReferencedContainer = "container:FlatButtons.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9644A3661776966F0008255E"
BuildableName = "FlatButtons.app"
BlueprintName = "FlatButtons"
ReferencedContainer = "container:FlatButtons.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>FlatButtons.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>9644A3661776966F0008255E</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
35 changes: 35 additions & 0 deletions FlatButtons/FlatButton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// FlatButton.h
// FlatButtons
//
// Created by JASON EVERETT on 6/22/13.
// Copyright (c) 2013 JASON EVERETT. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

#import <UIKit/UIKit.h>

@interface FlatButton : UIButton
@property(strong, nonatomic)UIColor *myColor;
-(id)initWithFrame:(CGRect)frame withBackgroundColor:(UIColor*)backgroundColor;
@end
Loading

0 comments on commit e589cfe

Please sign in to comment.