Skip to content

Commit

Permalink
Update code formatting and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
ole committed May 13, 2013
1 parent 090efc1 commit 56e399c
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 98 deletions.
20 changes: 3 additions & 17 deletions OBShapedButton/OBShapedButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,20 @@
October, 2009
*/

#import <UIKit/UIKit.h>


/*
OBShapedButton is a UIButton subclass optimized for non-rectangular button shapes.
Instances of OBShapedButton respond to touches only in areas where the image that is
assigned to the button for UIControlStateNormal is non-transparent.
Usage:
- Add OBShapedButton.h, OBShapedButton.m, UIImage+ColorAtPixel.h, and UIImage+ColorAtPixel.m
to your Xcode project.
- Design your UI in Interface Builder with UIButtons as usual. Set the Button type to Custom
and provide transparent PNG images for the different control states as needed.
- In the Identity Inspector in Interface Builder, set the Class of the button to OBShapedButton.
That's it! Your button will now only respond to touches where the PNG image for the normal
control state is non-transparent.
*/


#import <UIKit/UIKit.h>

// -[UIView hitTest:withEvent: ignores views that an alpha level less than 0.1.
// So we will do the same and treat pixels with alpha < 0.1 as transparent.
#define kAlphaVisibleThreshold (0.1f)

@interface OBShapedButton : UIButton

@interface OBShapedButton : UIButton {
// Our class interface is empty. OBShapedButton only overwrites one method of UIView.
// It has no attributes of its own.
}
// Class interface is empty. OBShapedButton does not add new public methods.

@end
22 changes: 12 additions & 10 deletions OBShapedButton/OBShapedButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,21 @@ of this software and associated documentation files (the "Software"), to deal
#import "OBShapedButton.h"
#import "UIImage+ColorAtPixel.h"


@interface OBShapedButton ()

@property (nonatomic, assign) CGPoint previousTouchPoint;
@property (nonatomic, assign) BOOL previousTouchHitTestResponse;
@property (nonatomic, strong) UIImage *buttonImage;
@property (nonatomic, strong) UIImage *buttonBackground;

- (void)updateImageCacheForCurrentState;
- (void)resetHitTestCache;

@end


@implementation OBShapedButton

@synthesize previousTouchPoint = _previousTouchPoint;
@synthesize previousTouchHitTestResponse = _previousTouchHitTestResponse;
@synthesize buttonImage = _buttonImage;
@synthesize buttonBackground = _buttonBackground;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
Expand Down Expand Up @@ -85,13 +80,15 @@ - (BOOL)isAlphaVisibleAtPoint:(CGPoint)point forImage:(UIImage *)image
CGFloat alpha = 0.0;

if ([pixelColor respondsToSelector:@selector(getRed:green:blue:alpha:)])
{// available from iOS 5.0
{
// available from iOS 5.0
[pixelColor getRed:NULL green:NULL blue:NULL alpha:&alpha];
}
else
{// for iOS < 5.0
// In iOS 6.1 the code is not working in release mode, it works only in debug
// CGColorGetAlpha always return 0.
{
// for iOS < 5.0
// In iOS 6.1 this code is not working in release mode, it works only in debug
// CGColorGetAlpha always return 0.
CGColorRef cgPixelColor = [pixelColor CGColor];
alpha = CGColorGetAlpha(cgPixelColor);
}
Expand Down Expand Up @@ -142,6 +139,8 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
}


#pragma mark - Accessors

// Reset the Hit Test Cache when a new image is assigned to the button
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
Expand Down Expand Up @@ -175,6 +174,9 @@ - (void)setSelected:(BOOL)selected
[self updateImageCacheForCurrentState];
}


#pragma mark - Helper methods

- (void)updateImageCacheForCurrentState
{
_buttonBackground = [self currentBackgroundImage];
Expand Down
6 changes: 1 addition & 5 deletions ShapedButtonDemo/Classes/ComplexShapesDemoViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@

#import <UIKit/UIKit.h>


@interface ComplexShapesDemoViewController : UIViewController {

}

@interface ComplexShapesDemoViewController : UIViewController
@end
40 changes: 0 additions & 40 deletions ShapedButtonDemo/Classes/ComplexShapesDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,5 @@

#import "ComplexShapesDemoViewController.h"


@implementation ComplexShapesDemoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
5 changes: 1 addition & 4 deletions ShapedButtonDemo/Classes/ShapedButtonDemoAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@

#import <UIKit/UIKit.h>

@interface ShapedButtonDemoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@interface ShapedButtonDemoAppDelegate : NSObject <UIApplicationDelegate>

@property (nonatomic, strong) IBOutlet UIWindow *window;
@property (nonatomic, strong) IBOutlet UITabBarController *tabBarController;
Expand Down
13 changes: 4 additions & 9 deletions ShapedButtonDemo/Classes/ShapedButtonDemoAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,10 @@ of this software and associated documentation files (the "Software"), to deal

@implementation ShapedButtonDemoAppDelegate

@synthesize window;
@synthesize tabBarController;


- (void)applicationDidFinishLaunching:(UIApplication * __attribute__((unused)))application {

// Override point for customization after app launch
[window addSubview:self.tabBarController.view];
[window makeKeyAndVisible];
- (void)applicationDidFinishLaunching:(UIApplication * __attribute__((unused)))application
{
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}

@end
6 changes: 1 addition & 5 deletions ShapedButtonDemo/Classes/ShapedButtonDemoViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,5 @@

#import <UIKit/UIKit.h>

@interface ShapedButtonDemoViewController : UIViewController {

}

@interface ShapedButtonDemoViewController : UIViewController
@end

2 changes: 0 additions & 2 deletions ShapedButtonDemo/Classes/ShapedButtonDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ of this software and associated documentation files (the "Software"), to deal

#import "ShapedButtonDemoViewController.h"


@implementation ShapedButtonDemoViewController

@end
4 changes: 2 additions & 2 deletions ShapedButtonDemo/ShapedButtonDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@
080E96DDFE201D6D7F000001 /* Classes */ = {
isa = PBXGroup;
children = (
2DEFB36C140B6D8F0098082E /* ComplexShapesDemoViewController.h */,
2DEFB36D140B6D8F0098082E /* ComplexShapesDemoViewController.m */,
1D3623240D0F684500981E51 /* ShapedButtonDemoAppDelegate.h */,
1D3623250D0F684500981E51 /* ShapedButtonDemoAppDelegate.m */,
28D7ACF60DDB3853001CB0EB /* ShapedButtonDemoViewController.h */,
28D7ACF70DDB3853001CB0EB /* ShapedButtonDemoViewController.m */,
2DEFB36C140B6D8F0098082E /* ComplexShapesDemoViewController.h */,
2DEFB36D140B6D8F0098082E /* ComplexShapesDemoViewController.m */,
);
path = Classes;
sourceTree = "<group>";
Expand Down
6 changes: 2 additions & 4 deletions UIImage+ColorAtPixel/UIImage+ColorAtPixel.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ of this software and associated documentation files (the "Software"), to deal
*/

#import <CoreGraphics/CoreGraphics.h>

#import "UIImage+ColorAtPixel.h"


@implementation UIImage (ColorAtPixel)

/*
Expand All @@ -45,13 +43,13 @@ If you need to query pixel colors for the same image repeatedly (e.g., in a loop
this approach is probably less efficient than drawing the entire image into memory
once and caching it.
*/
- (UIColor *)colorAtPixel:(CGPoint)point {
- (UIColor *)colorAtPixel:(CGPoint)point
{
// Cancel if point is outside image coordinates
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {
return nil;
}


// Create a 1x1 pixel byte array and bitmap context to draw the pixel into.
// Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
NSInteger pointX = trunc(point.x);
Expand Down

0 comments on commit 56e399c

Please sign in to comment.