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

Commit

Permalink
Turn all layers into @properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Reda Lemeden committed Oct 7, 2012
1 parent b62e08d commit 657ad60
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 53 deletions.
2 changes: 0 additions & 2 deletions Custom UIButtons/CBLayer.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@


@interface CBLayer : UIButton @interface CBLayer : UIButton


@property (assign, nonatomic) BOOL tapped;

@end @end
113 changes: 62 additions & 51 deletions Custom UIButtons/CBLayer.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@


@interface CBLayer () @interface CBLayer ()


@property (assign, nonatomic) BOOL setupLayers; @property (assign, nonatomic) BOOL tapped, setupLayers;
@property (strong, nonatomic) CAGradientLayer *backgroundLayer, *highlightBackgroundLayer;
@property (strong,nonatomic) CALayer *innerGlow;

@end @end


@implementation CBLayer @implementation CBLayer
Expand All @@ -13,80 +16,87 @@ - (void)drawRect:(CGRect)rect
{ {
if (!_setupLayers) if (!_setupLayers)
{ {
[self drawBorder]; self.layer.cornerRadius = 4.5f;
[self drawInnerGlow]; self.layer.masksToBounds = YES;
[self drawBackgroundGradient]; self.layer.borderWidth = 1;
[self drawHighlightedBackgroundGradient]; self.layer.borderColor = [UIColor colorWithRed:0.77f green:0.43f blue:0.00f alpha:1.00f].CGColor;

[self setInnerGlow];
[self setBackgroundLayer];
[self setHighlightBackgroundLayer];
} }


CAGradientLayer *highlightLayer = [self.layer.sublayers objectAtIndex:1];


if (_tapped) if (_tapped)
{ {
highlightLayer.hidden = NO; _highlightBackgroundLayer.hidden = NO;
} }


else else
{ {
highlightLayer.hidden = YES; _highlightBackgroundLayer.hidden = YES;
} }


_setupLayers = YES; _setupLayers = YES;
} }


- (void)drawBackgroundGradient #pragma mark - Layer setters
{
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = (@[
(id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor,
(id)[UIColor colorWithRed:0.91f green:0.58f blue:0.00f alpha:1.00f].CGColor
]);
gradient.locations = (@[
@0.0f,
@1.0f
]);

[self.layer insertSublayer:gradient atIndex:0];
}


- (void)drawHighlightedBackgroundGradient - (void)setBackgroundLayer
{ {
CAGradientLayer *gradient = [CAGradientLayer layer]; if (!_backgroundLayer)
gradient.frame = self.bounds; {
gradient.colors = (@[ _backgroundLayer = [CAGradientLayer layer];
(id)[UIColor colorWithRed:0.91f green:0.58f blue:0.00f alpha:1.00f].CGColor, _backgroundLayer.frame = self.bounds;
(id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor _backgroundLayer.colors = (@[
]); (id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor,
gradient.locations = (@[ (id)[UIColor colorWithRed:0.91f green:0.58f blue:0.00f alpha:1.00f].CGColor
@0.0f, ]);
@1.0f _backgroundLayer.locations = (@[
]); @0.0f,

@1.0f
[self.layer insertSublayer:gradient atIndex:1]; ]);

[self.layer insertSublayer:_backgroundLayer atIndex:0];
}
} }


- (void)drawInnerGlow - (void)setHighlightBackgroundLayer
{ {
CALayer *innerglow = [CALayer layer]; if (!_highlightBackgroundLayer)
CGRect innerGlowFrame = CGRectMake(self.bounds.origin.x+1, self.bounds.origin.y+1, self.bounds.size.width-2, self.bounds.size.height-2); {
innerglow.frame = innerGlowFrame; _highlightBackgroundLayer = [CAGradientLayer layer];
innerglow.cornerRadius= 4.5f; _highlightBackgroundLayer.frame = self.bounds;
innerglow.borderWidth = 1; _highlightBackgroundLayer.colors = (@[
innerglow.borderColor = [[UIColor whiteColor] CGColor]; (id)[UIColor colorWithRed:0.91f green:0.58f blue:0.00f alpha:1.00f].CGColor,
innerglow.opacity = 0.5; (id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor

]);
[self.layer insertSublayer:innerglow atIndex:2]; _highlightBackgroundLayer.locations = (@[
@0.0f,
@1.0f
]);
[self.layer insertSublayer:_highlightBackgroundLayer atIndex:1];
}
} }


- (void)drawBorder - (void)setInnerGlow
{ {
self.layer.cornerRadius = 4.5f; if (!_innerGlow)
self.layer.masksToBounds = YES; {
self.layer.borderWidth = 1; _innerGlow = [CALayer layer];
self.layer.borderColor = [UIColor colorWithRed:0.77f green:0.43f blue:0.00f alpha:1.00f].CGColor; CGRect innerGlowFrame = CGRectMake(self.bounds.origin.x+1, self.bounds.origin.y+1, self.bounds.size.width-2, self.bounds.size.height-2);
_innerGlow.frame = innerGlowFrame;
_innerGlow.cornerRadius= 4.5f;
_innerGlow.borderWidth = 1;
_innerGlow.borderColor = [[UIColor whiteColor] CGColor];
_innerGlow.opacity = 0.5;

[self.layer insertSublayer:_innerGlow atIndex:2];
}
} }


#pragma mark - Touch event overrides

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ {
_tapped = YES; _tapped = YES;
Expand Down Expand Up @@ -117,6 +127,7 @@ -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
_tapped = NO; _tapped = NO;
[self setNeedsDisplay]; [self setNeedsDisplay];
} }

[super touchesMoved:touches withEvent:event]; [super touchesMoved:touches withEvent:event];
} }


Expand Down

0 comments on commit 657ad60

Please sign in to comment.