Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit b92b8184aa880701c7fc73db2b03146e8aa4daf9 1 parent a24cca2
Matt Connolly mattconnolly authored

Showing 2 changed files with 38 additions and 38 deletions. Show diff stats Hide diff stats

  1. +0 4 CoolButtons/CoolButton.h
  2. +38 34 CoolButtons/CoolButton.m
4 CoolButtons/CoolButton.h
@@ -16,9 +16,5 @@
16 16 }
17 17
18 18 @property (nonatomic, retain)UIColor *buttonColor;
19   -@property (nonatomic, retain)UIView *innerView;
20   -@property (nonatomic, retain)CALayer *highlightLayer;
21   -
22   -- (void)setButtonColor:(UIColor *)buttonColor;
23 19
24 20 @end
72 CoolButtons/CoolButton.m
@@ -16,8 +16,6 @@ - (void) buildView;
16 16 @implementation CoolButton
17 17
18 18 @synthesize buttonColor=_buttonColor;
19   -@synthesize innerView=_innerView;
20   -@synthesize highlightLayer=_highlightLayer;
21 19
22 20 - (id)initWithCoder:(NSCoder *)coder {
23 21 self = [super initWithCoder:coder];
@@ -43,51 +41,58 @@ - (id)initWithFrame:(CGRect)frame
43 41
44 42 -(void) buildView
45 43 {
46   - self.innerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)] autorelease];
47   - [self.innerView setUserInteractionEnabled:false];
  44 + _innerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
  45 + [_innerView setUserInteractionEnabled:false];
48 46
49 47 [self addTarget:self action:@selector(addHighlight) forControlEvents:UIControlEventTouchDown];
50 48 [self addTarget:self action:@selector(removeHighlight) forControlEvents:UIControlEventTouchCancel|UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
51 49
52 50 if ([[self subviews] count] > 0)
53   - [self insertSubview:self.innerView belowSubview:[[self subviews] objectAtIndex:0]];
  51 + [self insertSubview:_innerView belowSubview:[[self subviews] objectAtIndex:0]];
54 52 else
55   - [self addSubview:self.innerView];
  53 + [self addSubview:_innerView];
56 54
57   - self.highlightLayer = [CALayer layer];
58   - [self.highlightLayer setAnchorPoint:CGPointMake(0, 0)];
59   - [self.highlightLayer setBounds:[self bounds]];
60   - [self.highlightLayer setBackgroundColor:[[UIColor colorWithWhite:0.0 alpha:0.3] CGColor]];
  55 + _highlightLayer = [[CALayer layer] retain];
  56 + [_highlightLayer setAnchorPoint:CGPointMake(0, 0)];
  57 + [_highlightLayer setBounds:[self bounds]];
  58 + [_highlightLayer setBackgroundColor:[[UIColor colorWithWhite:0.0 alpha:0.3] CGColor]];
61 59 }
62 60
63 61 -(void)addHighlight
64 62 {
65 63 NSLog(@"adding highlight layer");
66   - [[self.innerView layer] insertSublayer:self.highlightLayer atIndex:3];
  64 + [[_innerView layer] insertSublayer:_highlightLayer atIndex:3];
67 65 }
68 66
69 67 -(void)removeHighlight
70 68 {
71 69 NSLog(@"removing highlight layer");
72   - [self.highlightLayer removeFromSuperlayer];
  70 + [_highlightLayer removeFromSuperlayer];
73 71 }
74 72
75 73 - (void)setButtonColor:(UIColor *)value
76 74 {
77 75 [_buttonColor autorelease];
78 76 _buttonColor = [value retain];
79   - [self.innerView setBackgroundColor:[self buttonColor]];
  77 + [_innerView setBackgroundColor:[self buttonColor]];
  78 + [self setNeedsLayout];
80 79 }
81 80
82   --(void)drawRect:(CGRect)rect
  81 +- (void)layoutSubviews
83 82 {
84   - NSLog(@"drawing the rect!");
85   - while ([[[self.innerView layer] sublayers] count] > 0) {
86   - [[[[self.innerView layer] sublayers] objectAtIndex:0] removeFromSuperlayer];
  83 + [super layoutSubviews];
  84 +
  85 + // remember these layers so we don't have to call the views to get them repeatedly
  86 + CALayer* self_layer = [self layer];
  87 + CALayer* inner_layer = [_innerView layer];
  88 +
  89 + NSLog(@"resetting layers in layoutSubviews!");
  90 + while ([[inner_layer sublayers] count] > 0) {
  91 + [[[inner_layer sublayers] objectAtIndex:0] removeFromSuperlayer];
87 92 }
88 93
89 94 // create a view to store all the content
90   - [self.innerView setBackgroundColor:[self buttonColor]];
  95 + [_innerView setBackgroundColor:[self buttonColor]];
91 96
92 97 // create gradient layer
93 98 CAGradientLayer *gradientLayer = [CAGradientLayer layer];
@@ -97,7 +102,7 @@ -(void)drawRect:(CGRect)rect
97 102 [gradientLayer setColors:[NSArray arrayWithObjects:
98 103 (id)[[UIColor colorWithWhite:1.0 alpha:0.3] CGColor],
99 104 (id)[[UIColor colorWithWhite:1.0 alpha:0.10] CGColor], nil]];
100   - [[self.innerView layer] insertSublayer:gradientLayer atIndex:1];
  105 + [inner_layer insertSublayer:gradientLayer atIndex:1];
101 106
102 107 // create inner glow layer
103 108 CAGradientLayer *innerGlow = [CAGradientLayer layer];
@@ -109,30 +114,29 @@ -(void)drawRect:(CGRect)rect
109 114 [innerGlow setStartPoint:CGPointMake(0.5, 0.0)];
110 115 CGFloat innerGlowHeight = 1 - ((self.bounds.size.height-2) / self.bounds.size.height);
111 116 [innerGlow setEndPoint:CGPointMake(0.5, innerGlowHeight)];
112   - [[self.innerView layer] insertSublayer:innerGlow atIndex:2];
  117 + [inner_layer insertSublayer:innerGlow atIndex:2];
113 118
114 119 // create inner shadow layer - using a border for now as a hack
115 120 // TODO: Figure out how to use an axial gradient to accomplish this effect
116   - [[self.innerView layer] setBorderWidth:0.7];
117   - [[self.innerView layer] setBorderColor:[[UIColor colorWithWhite:0.0 alpha:0.3] CGColor]];
118   -
119   - [[self.innerView layer] setCornerRadius:5.0];
120   - [[self.innerView layer] setMasksToBounds:YES];
  121 + [inner_layer setBorderWidth:0.7];
  122 + [inner_layer setBorderColor:[[UIColor colorWithWhite:0.0 alpha:0.3] CGColor]];
  123 + [inner_layer setCornerRadius:5.0];
  124 + [inner_layer setMasksToBounds:YES];
121 125
122 126 // add a drop shadow to the layer
123   - [[self layer] setShadowOffset:CGSizeMake(0, 0.7)];
124   - [[self layer] setShadowColor:[[UIColor whiteColor] CGColor]];
125   - [[self layer] setShadowOpacity:0.5];
126   - [[self layer] setShadowRadius:0.5];
127   -
128   - [[self layer] setCornerRadius:5.0];
  127 + [self_layer setShadowOffset:CGSizeMake(0, 0.7)];
  128 + [self_layer setShadowColor:[[UIColor whiteColor] CGColor]];
  129 + [self_layer setShadowOpacity:0.5];
  130 + [self_layer setShadowRadius:0.5];
  131 + [self_layer setCornerRadius:5.0];
129 132 }
130 133
  134 +
131 135 -(void)dealloc
132 136 {
133   - self.innerView = nil;
134   - self.highlightLayer = nil;
135   - self.buttonColor = nil;
  137 + [_innerView release];
  138 + [_highlightLayer release];
  139 + [_buttonColor release];
136 140 [super dealloc];
137 141 }
138 142

0 comments on commit b92b818

Please sign in to comment.
Something went wrong with that request. Please try again.