Skip to content
This repository has been archived by the owner on Aug 31, 2019. It is now read-only.

Commit

Permalink
Remove unused layer masking
Browse files Browse the repository at this point in the history
- Fix formatting
- Add blog post link
  • Loading branch information
Reda Lemeden committed Oct 14, 2012
1 parent c69dbad commit 39d4ec2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
37 changes: 18 additions & 19 deletions Custom UIButtons/CBLayer.m
Expand Up @@ -17,34 +17,36 @@ + (CBLayer *)buttonWithType:(UIButtonType)type
return [super buttonWithType:UIButtonTypeCustom]; return [super buttonWithType:UIButtonTypeCustom];
} }


- (id) initWithCoder:(NSCoder *)coder { - (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder]; self = [super initWithCoder:coder];
if (self) {
if (self)
{
[self drawButton]; [self drawButton];
[self drawInnerGlow]; [self drawInnerGlow];
[self drawBackgroundLayer]; [self drawBackgroundLayer];
[self drawHighlightBackgroundLayer]; [self drawHighlightBackgroundLayer];


_highlightBackgroundLayer.hidden = YES; _highlightBackgroundLayer.hidden = YES;
} }

return self; return self;
} }


- (void) layoutSubviews - (void)layoutSubviews
{ {
// Set the frame (1pt inset) // Set layer frames
CGRect innerGlowFrame = CGRectInset(self.bounds, 1, 1); _innerGlow.frame = CGRectInset(self.bounds, 1, 1);
_innerGlow.frame = innerGlowFrame;

_backgroundLayer.frame = self.bounds; _backgroundLayer.frame = self.bounds;
_highlightBackgroundLayer.frame = self.bounds; _highlightBackgroundLayer.frame = self.bounds;


[super layoutSubviews]; [super layoutSubviews];
} }


- (void) setHighlighted:(BOOL)highlighted - (void)setHighlighted:(BOOL)highlighted
{ {
// set property without implicit animation // Set property without implicit animation
[CATransaction begin]; [CATransaction begin];
[CATransaction setDisableActions:YES]; [CATransaction setDisableActions:YES];
_highlightBackgroundLayer.hidden = !highlighted; _highlightBackgroundLayer.hidden = !highlighted;
Expand All @@ -57,12 +59,9 @@ - (void) setHighlighted:(BOOL)highlighted


- (void)drawButton - (void)drawButton
{ {
// Get the root layer (any UIView subclass comes with one) // Get the root layer (any UIView subclass comes with one)
CALayer *layer = self.layer; CALayer *layer = self.layer;


// Make the root layer act as a mask for all sublayers
layer.masksToBounds = YES;

layer.cornerRadius = 4.5f; layer.cornerRadius = 4.5f;
layer.borderWidth = 1; layer.borderWidth = 1;
layer.borderColor = [UIColor colorWithRed:0.77f green:0.43f blue:0.00f alpha:1.00f].CGColor; layer.borderColor = [UIColor colorWithRed:0.77f green:0.43f blue:0.00f alpha:1.00f].CGColor;
Expand Down Expand Up @@ -101,13 +100,13 @@ - (void)drawHighlightBackgroundLayer
{ {
_highlightBackgroundLayer = [CAGradientLayer layer]; _highlightBackgroundLayer = [CAGradientLayer layer];
_highlightBackgroundLayer.colors = (@[ _highlightBackgroundLayer.colors = (@[
(id)[UIColor colorWithRed:0.91f green:0.55f blue:0.00f alpha:1.00f].CGColor, (id)[UIColor colorWithRed:0.91f green:0.55f blue:0.00f alpha:1.00f].CGColor,
(id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor (id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor
]); ]);
_highlightBackgroundLayer.locations = (@[ _highlightBackgroundLayer.locations = (@[
@0.0f, @0.0f,
@1.0f @1.0f
]); ]);
[self.layer insertSublayer:_highlightBackgroundLayer atIndex:1]; [self.layer insertSublayer:_highlightBackgroundLayer atIndex:1];
} }
} }
Expand Down
12 changes: 1 addition & 11 deletions README.md
@@ -1,13 +1,3 @@
# Custom UIButton # Custom UIButton


A demo app showcasing the various methods of customizing a UIButton. Requires Xcode 4.5 and iOS 6. For more deatils, check out the accompanying blog post. A demo app showcasing the various methods of customizing a UIButton. Requires Xcode 4.5 and iOS 6. For more details, check out the accompanying [blog post](http://robots.thoughtbot.com/post/33427366406/designing-for-ios-taming-uibutton).

## modifications by kluivers

- CoreGraphics is not used, removed unnecessary `- (void) drawRect:(CGRect)rect`
- no custom event handling, uses `-(void)setHighlighted:(BOOL)highlighted` instead
- disabled implicit animations

Initialisation of layers now happens in - (id) initWithCoder:(NSCoder *)decoder, which is called after deserialization from a xib/storyboard. If you want to create the CBLayer in code another initialiser has to be added.

Layer creating is kept intact and exactly the same as the original implementation.

0 comments on commit 39d4ec2

Please sign in to comment.