22
33@interface CBLayer ()
44
5+ @property (assign , nonatomic ) BOOL setupLayers;
56@end
67
78@implementation CBLayer
89
910#pragma mark -
10- - (void )awakeFromNib
11- {
12-
13- }
1411
1512- (void )drawRect : (CGRect)rect
1613{
17- [self drawBorder ];
18- [self drawInnerGlow ];
19- [self drawBackgroundGradient ];
14+ if (!_setupLayers)
15+ {
16+ [self drawBorder ];
17+ [self drawInnerGlow ];
18+ [self drawBackgroundGradient ];
19+ [self drawHighlightedBackgroundGradient ];
20+ }
21+
22+ CAGradientLayer *highlightLayer = [self .layer.sublayers objectAtIndex: 1 ];
23+
24+ if (_tapped)
25+ {
26+ highlightLayer.hidden = NO ;
27+ }
28+
29+ else
30+ {
31+ highlightLayer.hidden = YES ;
32+ }
33+
34+ _setupLayers = YES ;
2035}
2136
2237- (void )drawBackgroundGradient
2338{
24- // Instantiate the gradient layer
2539 CAGradientLayer *gradient = [CAGradientLayer layer ];
26-
27- // Set the gradient frame (make the gradient fill the whole button)
2840 gradient.frame = self.bounds ;
29-
30-
31- // Set the colors
3241 gradient.colors = (@[
3342 (id )[UIColor colorWithRed: 0 .94f green: 0 .82f blue: 0 .52f alpha: 1 .00f ].CGColor,
3443 (id )[UIColor colorWithRed: 0 .91f green: 0 .58f blue: 0 .00f alpha: 1 .00f ].CGColor
3544 ]);
36-
37- // Set the stops
3845 gradient.locations = (@[
3946 @0 .0f ,
4047 @1 .0f
4148 ]);
4249
43- // Add the gradient to the layer hierarchy
4450 [self .layer insertSublayer: gradient atIndex: 0 ];
4551}
4652
47- - (void )drawBackgroundGradientHighlighted
53+ - (void )drawHighlightedBackgroundGradient
4854{
49- // Instantiate the gradient layer
5055 CAGradientLayer *gradient = [CAGradientLayer layer ];
51-
52- // Set the gradient frame (make the gradient fill the whole button)
5356 gradient.frame = self.bounds ;
54-
55- // Set the colors
5657 gradient.colors = (@[
57- (id )[UIColor colorWithRed: 0 .3f green: 0 .82f blue: 0 .52f alpha: 1 .00f ].CGColor,
58- (id )[UIColor colorWithRed: 0 .91f green: 0 .58f blue: 0 .00f alpha: 1 .00f ].CGColor
58+ (id )[UIColor colorWithRed: 0 .91f green: 0 .58f blue: 0 .00f alpha: 1 .00f ].CGColor,
59+ (id )[UIColor colorWithRed: 0 .94f green: 0 .82f blue: 0 .52f alpha: 1 .00f ].CGColor
5960 ]);
60-
61- // Set the stops
6261 gradient.locations = (@[
6362 @0 .0f ,
6463 @1 .0f
6564 ]);
66- // gradient.hidden = Yes;
67-
68- // Add the gradient to the layer hierarchy
69- [self .layer insertSublayer: gradient atIndex: 0 ];
65+
66+ [self .layer insertSublayer: gradient atIndex: 1 ];
7067}
7168
7269- (void )drawInnerGlow
@@ -79,7 +76,7 @@ - (void)drawInnerGlow
7976 innerglow.borderColor = [[UIColor whiteColor ] CGColor ];
8077 innerglow.opacity = 0.5 ;
8178
82- [self .layer insertSublayer: innerglow atIndex: 1 ];
79+ [self .layer insertSublayer: innerglow atIndex: 2 ];
8380}
8481
8582- (void )drawBorder
@@ -90,4 +87,37 @@ - (void)drawBorder
9087 self.layer .borderColor = [UIColor colorWithRed: 0 .77f green: 0 .43f blue: 0 .00f alpha: 1 .00f ].CGColor ;
9188}
9289
90+ -(void )touchesBegan : (NSSet *)touches withEvent : (UIEvent *)event
91+ {
92+ _tapped = YES ;
93+ [self setNeedsDisplay ];
94+ [super touchesBegan: touches withEvent: event];
95+ }
96+
97+ -(void )touchesEnded : (NSSet *)touches withEvent : (UIEvent *)event
98+ {
99+ _tapped = NO ;
100+ [self setNeedsDisplay ];
101+ [super touchesEnded: touches withEvent: event];
102+ }
103+
104+ -(void )touchesMoved : (NSSet *)touches withEvent : (UIEvent *)event
105+ {
106+ CGPoint touchPoint = [[touches anyObject ] locationInView: self ];
107+ CGRect testRect = CGRectMake (0 , 0 , self.frame .size .width , self.frame .size .height );
108+
109+ if (CGRectContainsPoint (testRect, touchPoint))
110+ {
111+ _tapped = YES ;
112+ [self setNeedsDisplay ];
113+ }
114+
115+ else
116+ {
117+ _tapped = NO ;
118+ [self setNeedsDisplay ];
119+ }
120+ [super touchesMoved: touches withEvent: event];
121+ }
122+
93123@end
0 commit comments