Permalink
Browse files

Removing unnecessary properties; Modifying code to adjust layers in -…

…layoutSubviews method, which provides a run-time optimisation by not requiring a -drawRect: method.
  • Loading branch information...
1 parent a24cca2 commit b92b8184aa880701c7fc73db2b03146e8aa4daf9 @mattconnolly mattconnolly committed Oct 17, 2011
Showing with 38 additions and 38 deletions.
  1. +0 −4 CoolButtons/CoolButton.h
  2. +38 −34 CoolButtons/CoolButton.m
View
4 CoolButtons/CoolButton.h
@@ -16,9 +16,5 @@
}
@property (nonatomic, retain)UIColor *buttonColor;
-@property (nonatomic, retain)UIView *innerView;
-@property (nonatomic, retain)CALayer *highlightLayer;
-
-- (void)setButtonColor:(UIColor *)buttonColor;
@end
View
72 CoolButtons/CoolButton.m
@@ -16,8 +16,6 @@ - (void) buildView;
@implementation CoolButton
@synthesize buttonColor=_buttonColor;
-@synthesize innerView=_innerView;
-@synthesize highlightLayer=_highlightLayer;
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
@@ -43,51 +41,58 @@ - (id)initWithFrame:(CGRect)frame
-(void) buildView
{
- self.innerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)] autorelease];
- [self.innerView setUserInteractionEnabled:false];
+ _innerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
+ [_innerView setUserInteractionEnabled:false];
[self addTarget:self action:@selector(addHighlight) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(removeHighlight) forControlEvents:UIControlEventTouchCancel|UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
if ([[self subviews] count] > 0)
- [self insertSubview:self.innerView belowSubview:[[self subviews] objectAtIndex:0]];
+ [self insertSubview:_innerView belowSubview:[[self subviews] objectAtIndex:0]];
else
- [self addSubview:self.innerView];
+ [self addSubview:_innerView];
- self.highlightLayer = [CALayer layer];
- [self.highlightLayer setAnchorPoint:CGPointMake(0, 0)];
- [self.highlightLayer setBounds:[self bounds]];
- [self.highlightLayer setBackgroundColor:[[UIColor colorWithWhite:0.0 alpha:0.3] CGColor]];
+ _highlightLayer = [[CALayer layer] retain];
+ [_highlightLayer setAnchorPoint:CGPointMake(0, 0)];
+ [_highlightLayer setBounds:[self bounds]];
+ [_highlightLayer setBackgroundColor:[[UIColor colorWithWhite:0.0 alpha:0.3] CGColor]];
}
-(void)addHighlight
{
NSLog(@"adding highlight layer");
- [[self.innerView layer] insertSublayer:self.highlightLayer atIndex:3];
+ [[_innerView layer] insertSublayer:_highlightLayer atIndex:3];
}
-(void)removeHighlight
{
NSLog(@"removing highlight layer");
- [self.highlightLayer removeFromSuperlayer];
+ [_highlightLayer removeFromSuperlayer];
}
- (void)setButtonColor:(UIColor *)value
{
[_buttonColor autorelease];
_buttonColor = [value retain];
- [self.innerView setBackgroundColor:[self buttonColor]];
+ [_innerView setBackgroundColor:[self buttonColor]];
+ [self setNeedsLayout];
}
--(void)drawRect:(CGRect)rect
+- (void)layoutSubviews
{
- NSLog(@"drawing the rect!");
- while ([[[self.innerView layer] sublayers] count] > 0) {
- [[[[self.innerView layer] sublayers] objectAtIndex:0] removeFromSuperlayer];
+ [super layoutSubviews];
+
+ // remember these layers so we don't have to call the views to get them repeatedly
+ CALayer* self_layer = [self layer];
+ CALayer* inner_layer = [_innerView layer];
+
+ NSLog(@"resetting layers in layoutSubviews!");
+ while ([[inner_layer sublayers] count] > 0) {
+ [[[inner_layer sublayers] objectAtIndex:0] removeFromSuperlayer];
}
// create a view to store all the content
- [self.innerView setBackgroundColor:[self buttonColor]];
+ [_innerView setBackgroundColor:[self buttonColor]];
// create gradient layer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
@@ -97,7 +102,7 @@ -(void)drawRect:(CGRect)rect
[gradientLayer setColors:[NSArray arrayWithObjects:
(id)[[UIColor colorWithWhite:1.0 alpha:0.3] CGColor],
(id)[[UIColor colorWithWhite:1.0 alpha:0.10] CGColor], nil]];
- [[self.innerView layer] insertSublayer:gradientLayer atIndex:1];
+ [inner_layer insertSublayer:gradientLayer atIndex:1];
// create inner glow layer
CAGradientLayer *innerGlow = [CAGradientLayer layer];
@@ -109,30 +114,29 @@ -(void)drawRect:(CGRect)rect
[innerGlow setStartPoint:CGPointMake(0.5, 0.0)];
CGFloat innerGlowHeight = 1 - ((self.bounds.size.height-2) / self.bounds.size.height);
[innerGlow setEndPoint:CGPointMake(0.5, innerGlowHeight)];
- [[self.innerView layer] insertSublayer:innerGlow atIndex:2];
+ [inner_layer insertSublayer:innerGlow atIndex:2];
// create inner shadow layer - using a border for now as a hack
// TODO: Figure out how to use an axial gradient to accomplish this effect
- [[self.innerView layer] setBorderWidth:0.7];
- [[self.innerView layer] setBorderColor:[[UIColor colorWithWhite:0.0 alpha:0.3] CGColor]];
-
- [[self.innerView layer] setCornerRadius:5.0];
- [[self.innerView layer] setMasksToBounds:YES];
+ [inner_layer setBorderWidth:0.7];
+ [inner_layer setBorderColor:[[UIColor colorWithWhite:0.0 alpha:0.3] CGColor]];
+ [inner_layer setCornerRadius:5.0];
+ [inner_layer setMasksToBounds:YES];
// add a drop shadow to the layer
- [[self layer] setShadowOffset:CGSizeMake(0, 0.7)];
- [[self layer] setShadowColor:[[UIColor whiteColor] CGColor]];
- [[self layer] setShadowOpacity:0.5];
- [[self layer] setShadowRadius:0.5];
-
- [[self layer] setCornerRadius:5.0];
+ [self_layer setShadowOffset:CGSizeMake(0, 0.7)];
+ [self_layer setShadowColor:[[UIColor whiteColor] CGColor]];
+ [self_layer setShadowOpacity:0.5];
+ [self_layer setShadowRadius:0.5];
+ [self_layer setCornerRadius:5.0];
}
+
-(void)dealloc
{
- self.innerView = nil;
- self.highlightLayer = nil;
- self.buttonColor = nil;
+ [_innerView release];
+ [_highlightLayer release];
+ [_buttonColor release];
[super dealloc];
}

0 comments on commit b92b818

Please sign in to comment.