Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add comments and rename pragmas

  • Loading branch information...
commit c5c875c89454fba99d16a938e25db34e1cc426dd 1 parent 631f598
@kaishin authored
View
18 Custom UIButtons/CBHybrid.m
@@ -2,12 +2,18 @@
@implementation CBHybrid
+
+// Resizable background image for normal state
static UIImage *gBackgroundImage;
+
+// Resizable background image for highlighted state
static UIImage *gBackgroundImageHighlighted;
+
+// Background image border radius and height
static int borderRadius = 5;
static int height = 37;
-#pragma mark - UIButton Overrides
+#pragma mark - Overrides
+ (CBHybrid *)buttonWithType:(UIButtonType)type
{
@@ -23,23 +29,27 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
return self;
}
-#pragma mark - Touch event overrides
+#pragma mark - Helper Methods
- (void)setupBackgrounds {
+ // Generate background images if necessary
if (!gBackgroundImage && !gBackgroundImageHighlighted) {
gBackgroundImage = [[self drawBackgroundImageHighlighted:NO] resizableImageWithCapInsets:UIEdgeInsetsMake(borderRadius, borderRadius, borderRadius, borderRadius) resizingMode:UIImageResizingModeStretch];
gBackgroundImageHighlighted = [[self drawBackgroundImageHighlighted:YES] resizableImageWithCapInsets:UIEdgeInsetsMake(borderRadius, borderRadius, borderRadius, borderRadius) resizingMode:UIImageResizingModeStretch];
}
+ // Set background for the button instance
[self setBackgroundImage:gBackgroundImage forState:UIControlStateNormal];
[self setBackgroundImage:gBackgroundImageHighlighted forState:UIControlStateHighlighted];
}
- (UIImage *)drawBackgroundImageHighlighted:(BOOL)highlighted {
+ // Get image width with a 1pt stretchable area
float width = 1 + (borderRadius * 2);
+ // Create bitmap context and color space
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
@@ -68,6 +78,7 @@ - (UIImage *)drawBackgroundImageHighlighted:(BOOL)highlighted {
// Draw rounded rectangle bezier path
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, width, height) cornerRadius: borderRadius];
+
// Use the bezier as a clipping path
[roundedRectanglePath addClip];
@@ -91,9 +102,8 @@ - (UIImage *)drawBackgroundImageHighlighted:(BOOL)highlighted {
// Output as Image
UIImage* backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
// Cleanup
+ UIGraphicsEndImageContext();
CGGradientRelease(gradient);
CGGradientRelease(highlightedGradient);
CGColorSpaceRelease(colorSpace);
View
18 Custom UIButtons/en.lproj/MainStoryboard.storyboard
@@ -113,24 +113,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="CBHybrid" superclassName="UIButton">
- <source key="sourceIdentifier" type="project" relativePath="./Classes/CBHybrid.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>
- </classes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
Please sign in to comment.
Something went wrong with that request. Please try again.