Permalink
Browse files

Rewrote CALayer based implementation. Does not use -(void)drawRect: a…

…nymore. Also removed custom event handling in favor of -(void)setHighlighted:
  • Loading branch information...
1 parent 60173df commit 05de544f9e0baa9f57fb5260ec92b443ab585535 @kluivers kluivers committed Oct 13, 2012
Showing with 28 additions and 73 deletions.
  1. +28 −73 Custom UIButtons/CBLayer.m
@@ -17,77 +17,40 @@ + (CBLayer *)buttonWithType:(UIButtonType)type
return [super buttonWithType:UIButtonTypeCustom];
}
-#pragma mark - Touch event overrides
-
--(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
-{
- // Set property
- _tapped = YES;
-
- // Force drawRect to be called (NEVER, EVER call drawRect directly)
- [self setNeedsDisplay];
-
- // Superclass implementation
- [super touchesBegan:touches withEvent:event];
-}
-
--(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
-{
- _tapped = NO;
- [self setNeedsDisplay];
- [super touchesEnded:touches withEvent:event];
+- (id) initWithCoder:(NSCoder *)coder {
+ self = [super initWithCoder:coder];
+ if (self) {
+ [self drawButton];
+ [self drawInnerGlow];
+ [self drawBackgroundLayer];
+ [self drawHighlightBackgroundLayer];
+
+ _highlightBackgroundLayer.hidden = YES;
+ }
+ return self;
}
-// The distance from the button beyond which the former should no longer be considered tapped
-static CGFloat touchDistance = 70;
-
--(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
+- (void) layoutSubviews
{
- // Get the distance between the touch point and the button bounds
- CGPoint touchPoint = [[touches anyObject] locationInView:self];
-
- // Define the touch area frame using the touch distance defined above
- CGRect touchArea = CGRectMake(0, - touchDistance, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) + touchDistance * 2);
-
- if (CGRectContainsPoint(touchArea, touchPoint))
- {
- _tapped = YES;
- [self setNeedsDisplay];
- }
-
- else
- {
- _tapped = NO;
- [self setNeedsDisplay];
- }
-
- [super touchesMoved:touches withEvent:event];
+ // Set the frame (1pt inset)
+ CGRect innerGlowFrame = CGRectInset(self.bounds, 1, 1);
+ _innerGlow.frame = innerGlowFrame;
+
+ _backgroundLayer.frame = self.bounds;
+ _highlightBackgroundLayer.frame = self.bounds;
+
+ [super layoutSubviews];
}
-#pragma mark -
-
-- (void)drawRect:(CGRect)rect
+- (void) setHighlighted:(BOOL)highlighted
{
- if (!_setupLayers)
- {
- [self drawButton];
- [self drawInnerGlow];
- [self drawBackgroundLayer];
- [self drawHighlightBackgroundLayer];
- }
-
- // Hide and show the highlighted background (inverted gradient)
- if (_tapped)
- {
- _highlightBackgroundLayer.hidden = NO;
- }
-
- else
- {
- _highlightBackgroundLayer.hidden = YES;
- }
-
- _setupLayers = YES;
+ // set property without implicit animation
+ [CATransaction begin];
+ [CATransaction setDisableActions:YES];
+ _highlightBackgroundLayer.hidden = !highlighted;
+ [CATransaction commit];
+
+ [super setHighlighted:highlighted];
}
#pragma mark - Layer setters
@@ -113,9 +76,6 @@ - (void)drawBackgroundLayer
// Instantiate the gradient layer
_backgroundLayer = [CAGradientLayer layer];
- // Set the gradient frame (make the gradient fill the whole button)
- _backgroundLayer.frame = self.bounds;
-
// Set the colors
_backgroundLayer.colors = (@[
(id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor,
@@ -140,7 +100,6 @@ - (void)drawHighlightBackgroundLayer
if (!_highlightBackgroundLayer)
{
_highlightBackgroundLayer = [CAGradientLayer layer];
- _highlightBackgroundLayer.frame = self.bounds;
_highlightBackgroundLayer.colors = (@[
(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
@@ -160,10 +119,6 @@ - (void)drawInnerGlow
// Instantiate the innerGlow layer
_innerGlow = [CALayer layer];
- // Set the frame (1pt inset)
- CGRect innerGlowFrame = CGRectInset(self.bounds, 1, 1);
- _innerGlow.frame = innerGlowFrame;
-
_innerGlow.cornerRadius= 4.5f;
_innerGlow.borderWidth = 1;
_innerGlow.borderColor = [[UIColor whiteColor] CGColor];

0 comments on commit 05de544

Please sign in to comment.