Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Turn all layers into @properties

  • Loading branch information...
commit 657ad60cdfc0cb14935a64ea177bfe3146e8d142 1 parent b62e08d
@kaishin authored
Showing with 62 additions and 53 deletions.
  1. +0 −2  Custom UIButtons/CBLayer.h
  2. +62 −51 Custom UIButtons/CBLayer.m
View
2  Custom UIButtons/CBLayer.h
@@ -3,6 +3,4 @@
@interface CBLayer : UIButton
-@property (assign, nonatomic) BOOL tapped;
-
@end
View
113 Custom UIButtons/CBLayer.m
@@ -2,7 +2,10 @@
@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
@implementation CBLayer
@@ -13,80 +16,87 @@ - (void)drawRect:(CGRect)rect
{
if (!_setupLayers)
{
- [self drawBorder];
- [self drawInnerGlow];
- [self drawBackgroundGradient];
- [self drawHighlightedBackgroundGradient];
+ self.layer.cornerRadius = 4.5f;
+ self.layer.masksToBounds = YES;
+ self.layer.borderWidth = 1;
+ 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)
{
- highlightLayer.hidden = NO;
+ _highlightBackgroundLayer.hidden = NO;
}
else
{
- highlightLayer.hidden = YES;
+ _highlightBackgroundLayer.hidden = YES;
}
_setupLayers = YES;
}
-- (void)drawBackgroundGradient
-{
- 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];
-}
+#pragma mark - Layer setters
-- (void)drawHighlightedBackgroundGradient
+- (void)setBackgroundLayer
{
- CAGradientLayer *gradient = [CAGradientLayer layer];
- gradient.frame = self.bounds;
- gradient.colors = (@[
- (id)[UIColor colorWithRed:0.91f green:0.58f blue:0.00f alpha:1.00f].CGColor,
- (id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor
- ]);
- gradient.locations = (@[
- @0.0f,
- @1.0f
- ]);
-
- [self.layer insertSublayer:gradient atIndex:1];
+ if (!_backgroundLayer)
+ {
+ _backgroundLayer = [CAGradientLayer layer];
+ _backgroundLayer.frame = self.bounds;
+ _backgroundLayer.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
+ ]);
+ _backgroundLayer.locations = (@[
+ @0.0f,
+ @1.0f
+ ]);
+
+ [self.layer insertSublayer:_backgroundLayer atIndex:0];
+ }
}
-- (void)drawInnerGlow
+- (void)setHighlightBackgroundLayer
{
- CALayer *innerglow = [CALayer layer];
- 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];
+ if (!_highlightBackgroundLayer)
+ {
+ _highlightBackgroundLayer = [CAGradientLayer layer];
+ _highlightBackgroundLayer.frame = self.bounds;
+ _highlightBackgroundLayer.colors = (@[
+ (id)[UIColor colorWithRed:0.91f green:0.58f blue:0.00f alpha:1.00f].CGColor,
+ (id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor
+ ]);
+ _highlightBackgroundLayer.locations = (@[
+ @0.0f,
+ @1.0f
+ ]);
+ [self.layer insertSublayer:_highlightBackgroundLayer atIndex:1];
+ }
}
-- (void)drawBorder
+- (void)setInnerGlow
{
- self.layer.cornerRadius = 4.5f;
- self.layer.masksToBounds = YES;
- self.layer.borderWidth = 1;
- self.layer.borderColor = [UIColor colorWithRed:0.77f green:0.43f blue:0.00f alpha:1.00f].CGColor;
+ if (!_innerGlow)
+ {
+ _innerGlow = [CALayer layer];
+ 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
{
_tapped = YES;
@@ -117,6 +127,7 @@ -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
_tapped = NO;
[self setNeedsDisplay];
}
+
[super touchesMoved:touches withEvent:event];
}
Please sign in to comment.
Something went wrong with that request. Please try again.