Permalink
Browse files

Implement CBHybrid

  • Loading branch information...
1 parent 473251d commit b6b717703e63e61451c7224da74149c2ff50881b Reda Lemeden committed Nov 16, 2012
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 9D39A8E91655D90D00943163 /* CBHybrid.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D39A8E81655D90D00943163 /* CBHybrid.m */; };
9D5AAD8B1618F5F90000EF26 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9D5AAD8A1618F5F90000EF26 /* UIKit.framework */; };
9D5AAD8D1618F5F90000EF26 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9D5AAD8C1618F5F90000EF26 /* Foundation.framework */; };
9D5AAD8F1618F5F90000EF26 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9D5AAD8E1618F5F90000EF26 /* CoreGraphics.framework */; };
@@ -57,6 +58,8 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 9D39A8E71655D90D00943163 /* CBHybrid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CBHybrid.h; sourceTree = "<group>"; };
+ 9D39A8E81655D90D00943163 /* CBHybrid.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CBHybrid.m; sourceTree = "<group>"; };
9D5AAD861618F5F90000EF26 /* Custom UIButtons.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Custom UIButtons.app"; sourceTree = BUILT_PRODUCTS_DIR; };
9D5AAD8A1618F5F90000EF26 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
9D5AAD8C1618F5F90000EF26 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -170,6 +173,8 @@
9DDA94F4161F68C90099D9C3 /* CBLayer.m */,
9DAEF93C1621E71B002B0242 /* CBBezier.h */,
9DAEF93D1621E71B002B0242 /* CBBezier.m */,
+ 9D39A8E71655D90D00943163 /* CBHybrid.h */,
+ 9D39A8E81655D90D00943163 /* CBHybrid.m */,
9D5AADA51618F5F90000EF26 /* CBViewController.h */,
9D5AADA61618F5F90000EF26 /* CBViewController.m */,
9D5AAD911618F5F90000EF26 /* Supporting Files */,
@@ -370,6 +375,7 @@
9D5AADA71618F5F90000EF26 /* CBViewController.m in Sources */,
9DDA94F5161F68C90099D9C3 /* CBLayer.m in Sources */,
9DAEF93E1621E71B002B0242 /* CBBezier.m in Sources */,
+ 9D39A8E91655D90D00943163 /* CBHybrid.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,5 @@
+#import <UIKit/UIKit.h>
+
+@interface CBHybrid : UIButton
+
+@end
View
@@ -0,0 +1,104 @@
+#import "CBHybrid.h"
+
+@implementation CBHybrid
+
+static UIImage *gBackgroundImage;
+static UIImage *gBackgroundImageHighlighted;
+static int borderRadius = 5;
+static int height = 37;
+
+#pragma mark - UIButton Overrides
+
++ (CBHybrid *)buttonWithType:(UIButtonType)type
+{
+ return [super buttonWithType:UIButtonTypeCustom];
+}
+
+- (id)initWithCoder:(NSCoder *)aDecoder {
+ self = [super initWithCoder:aDecoder];
+ if (self) {
+ [self setupBackgrounds];
+ }
+
+ return self;
+}
+
+#pragma mark - Touch event overrides
+
+- (void)setupBackgrounds {
+
+ 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];
+ }
+
+ [self setBackgroundImage:gBackgroundImage forState:UIControlStateNormal];
+ [self setBackgroundImage:gBackgroundImageHighlighted forState:UIControlStateHighlighted];
+}
+
+- (UIImage *)drawBackgroundImageHighlighted:(BOOL)highlighted {
+
+ float width = 1 + (borderRadius * 2);
+
+ UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0);
+ CGContextRef context = UIGraphicsGetCurrentContext();
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+
+ // Color Declarations
+ UIColor *borderColor = [UIColor colorWithRed:0.77f green:0.43f blue:0.00f alpha:1.00f];
+ UIColor *topColor = [UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f];
+ UIColor *bottomColor = [UIColor colorWithRed:0.91f green:0.55f blue:0.00f alpha:1.00f];
+ UIColor *innerGlow = [UIColor colorWithWhite:1.0 alpha:0.5];
+
+ // Gradient Declarations
+ NSArray *gradientColors = (@[
+ (id)topColor.CGColor,
+ (id)bottomColor.CGColor
+ ]);
+
+ CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)(gradientColors), NULL);
+
+ NSArray *highlightedGradientColors = (@[
+ (id)bottomColor.CGColor,
+ (id)topColor.CGColor
+ ]);
+
+ CGGradientRef highlightedGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)(highlightedGradientColors), NULL);
+
+
+ // 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];
+
+ // Use one of the two gradients depending on the state of the button
+ CGGradientRef background = highlighted? highlightedGradient : gradient;
+
+ // Draw gradient within the path
+ CGContextDrawLinearGradient(context, background, CGPointMake(140, 0), CGPointMake(140, height-1), 0);
+
+ // Draw border
+ [borderColor setStroke];
+ roundedRectanglePath.lineWidth = 2;
+ [roundedRectanglePath stroke];
+
+ // Draw Inner Glow
+ UIBezierPath *innerGlowRect = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(1.5, 1.5, width - 3, height - 3) cornerRadius: borderRadius * 4/5];
+ [innerGlow setStroke];
+ innerGlowRect.lineWidth = 1;
+ [innerGlowRect stroke];
+
+ // Output as Image
+ UIImage* backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
+
+ UIGraphicsEndImageContext();
+
+ // Cleanup
+ CGGradientRelease(gradient);
+ CGGradientRelease(highlightedGradient);
+ CGColorSpaceRelease(colorSpace);
+
+ return backgroundImage;
+}
+
+@end
Oops, something went wrong.

0 comments on commit b6b7177

Please sign in to comment.