Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enable visual interaction with user taps

  • Loading branch information...
commit b62e08d866d9c61322cfb4ca2401e38f35a9242c 1 parent 1a6448b
@kaishin authored
Showing with 63 additions and 31 deletions.
  1. +2 −0  Custom UIButtons/CBLayer.h
  2. +61 −31 Custom UIButtons/CBLayer.m
View
2  Custom UIButtons/CBLayer.h
@@ -3,4 +3,6 @@
@interface CBLayer : UIButton
+@property (assign, nonatomic) BOOL tapped;
+
@end
View
92 Custom UIButtons/CBLayer.m
@@ -2,71 +2,68 @@
@interface CBLayer ()
+@property (assign, nonatomic) BOOL setupLayers;
@end
@implementation CBLayer
#pragma mark -
-- (void)awakeFromNib
-{
-
-}
- (void)drawRect:(CGRect)rect
{
- [self drawBorder];
- [self drawInnerGlow];
- [self drawBackgroundGradient];
+ if (!_setupLayers)
+ {
+ [self drawBorder];
+ [self drawInnerGlow];
+ [self drawBackgroundGradient];
+ [self drawHighlightedBackgroundGradient];
+ }
+
+ CAGradientLayer *highlightLayer = [self.layer.sublayers objectAtIndex:1];
+
+ if (_tapped)
+ {
+ highlightLayer.hidden = NO;
+ }
+
+ else
+ {
+ highlightLayer.hidden = YES;
+ }
+
+ _setupLayers = YES;
}
- (void)drawBackgroundGradient
{
- // Instantiate the gradient layer
CAGradientLayer *gradient = [CAGradientLayer layer];
-
- // Set the gradient frame (make the gradient fill the whole button)
gradient.frame = self.bounds;
-
-
- // Set the colors
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
]);
-
- // Set the stops
gradient.locations = (@[
@0.0f,
@1.0f
]);
- // Add the gradient to the layer hierarchy
[self.layer insertSublayer:gradient atIndex:0];
}
-- (void)drawBackgroundGradientHighlighted
+- (void)drawHighlightedBackgroundGradient
{
- // Instantiate the gradient layer
CAGradientLayer *gradient = [CAGradientLayer layer];
-
- // Set the gradient frame (make the gradient fill the whole button)
gradient.frame = self.bounds;
-
- // Set the colors
gradient.colors = (@[
- (id)[UIColor colorWithRed:0.3f 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
+ (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
]);
-
- // Set the stops
gradient.locations = (@[
@0.0f,
@1.0f
]);
- //gradient.hidden = Yes;
-
- // Add the gradient to the layer hierarchy
- [self.layer insertSublayer:gradient atIndex:0];
+
+ [self.layer insertSublayer:gradient atIndex:1];
}
- (void)drawInnerGlow
@@ -79,7 +76,7 @@ - (void)drawInnerGlow
innerglow.borderColor = [[UIColor whiteColor] CGColor];
innerglow.opacity = 0.5;
- [self.layer insertSublayer:innerglow atIndex:1];
+ [self.layer insertSublayer:innerglow atIndex:2];
}
- (void)drawBorder
@@ -90,4 +87,37 @@ - (void)drawBorder
self.layer.borderColor = [UIColor colorWithRed:0.77f green:0.43f blue:0.00f alpha:1.00f].CGColor;
}
+-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
+{
+ _tapped = YES;
+ [self setNeedsDisplay];
+ [super touchesBegan:touches withEvent:event];
+}
+
+-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
+{
+ _tapped = NO;
+ [self setNeedsDisplay];
+ [super touchesEnded:touches withEvent:event];
+}
+
+-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
+{
+ CGPoint touchPoint = [[touches anyObject] locationInView:self];
+ CGRect testRect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
+
+ if (CGRectContainsPoint(testRect, touchPoint))
+ {
+ _tapped = YES;
+ [self setNeedsDisplay];
+ }
+
+ else
+ {
+ _tapped = NO;
+ [self setNeedsDisplay];
+ }
+ [super touchesMoved:touches withEvent:event];
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.