Permalink
Browse files

Add comments and implement drawButton()

  • Loading branch information...
1 parent 77b3758 commit a1e26544872b6d4ee797b94e482a342deaf98e69 Reda Lemeden committed Oct 8, 2012
Showing with 23 additions and 25 deletions.
  1. +23 −7 Custom UIButtons/CBLayer.m
  2. +0 −18 Custom UIButtons/en.lproj/MainStoryboard.storyboard
View
@@ -21,8 +21,13 @@ + (CBLayer *)buttonWithType:(UIButtonType)type
-(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];
}
@@ -35,8 +40,10 @@ -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
+ // Get the distance between the touch point and the button bounds
CGPoint touchPoint = [[touches anyObject] locationInView:self];
+ // Check if the point is contained within the button bounds
if (CGRectContainsPoint(self.bounds, touchPoint))
{
_tapped = YES;
@@ -58,17 +65,13 @@ - (void)drawRect:(CGRect)rect
{
if (!_setupLayers)
{
- 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 drawButton];
[self drawInnerGlow];
[self drawBackgroundLayer];
[self drawHighlightBackgroundLayer];
}
-
-
+
+ // Hide and show the highlighted background (inverted gradient)
if (_tapped)
{
_highlightBackgroundLayer.hidden = NO;
@@ -84,6 +87,19 @@ - (void)drawRect:(CGRect)rect
#pragma mark - Layer setters
+- (void)drawButton
+{
+ // Get the root layer (any UIView subclass comes with one)
+ CALayer *layer = self.layer;
+
+ // Make the root layer act as a mask for all sublayers
+ layer.masksToBounds = YES;
+
+ layer.cornerRadius = 4.5f;
+ layer.borderWidth = 1;
+ layer.borderColor = [UIColor colorWithRed:0.77f green:0.43f blue:0.00f alpha:1.00f].CGColor;
+}
+
- (void)drawBackgroundLayer
{
// Check if the property has been set already
@@ -127,24 +127,6 @@
<image name="button.png" width="280" height="36"/>
<image name="buttonHighlighted.png" width="280" height="36"/>
</resources>
- <classes>
- <class className="CBBezier" superclassName="UIButton">
- <source key="sourceIdentifier" type="project" relativePath="./Classes/CBBezier.h"/>
- </class>
- <class className="CBLayer" superclassName="UIButton">
- <source key="sourceIdentifier" type="project" relativePath="./Classes/CBLayer.h"/>
- </class>
- <class className="CBViewController" superclassName="UIViewController">
- <source key="sourceIdentifier" type="project" relativePath="./Classes/CBViewController.h"/>
- <relationships>
- <relationship kind="outlet" name="fullSizeImage" candidateClass="UIButton"/>
- <relationship kind="outlet" name="resizableImage" candidateClass="UIButton"/>
- </relationships>
- </class>
- <class className="NSLayoutConstraint" superclassName="NSObject">
- <source key="sourceIdentifier" type="project" relativePath="./Classes/NSLayoutConstraint.h"/>
- </class>
- </classes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>

0 comments on commit a1e2654

Please sign in to comment.