Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #12 from phillbaker/selectedimage

Add selected image to tab bar item.
  • Loading branch information...
commit c228a81503868f36e165c89d86d2cda504dd83fb 2 parents ed7c287 + f0ae69f
@myell0w myell0w authored
View
1  NGTabBarController/NGTabBarItem.h
@@ -11,6 +11,7 @@
@interface NGTabBarItem : UIControl
@property (nonatomic, strong) UIImage *image;
+@property (nonatomic, strong) UIImage *selectedImage;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UIColor *selectedImageTintColor;
View
75 NGTabBarController/NGTabBarItem.m
@@ -22,6 +22,7 @@ @implementation NGTabBarItem
@synthesize titleColor = _titleColor;
@synthesize selectedTitleColor = _selectedTitleColor;
@synthesize image = _image;
+@synthesize selectedImage = _selectedImage;
@synthesize titleLabel = _titleLabel;
////////////////////////////////////////////////////////////////////////
@@ -93,40 +94,46 @@ - (void)drawRect:(CGRect)rect {
// draw either a selection gradient/glow or a regular image
if (_selectedByUser) {
- // setup shadow
- CGSize shadowOffset = CGSizeMake(0.0f, 1.0f);
- CGFloat shadowBlur = 3.0;
- CGColorRef cgShadowColor = [[UIColor blackColor] CGColor];
-
- // setup gradient
- CGFloat alpha0 = 0.8;
- CGFloat alpha1 = 0.6;
- CGFloat alpha2 = 0.0;
- CGFloat alpha3 = 0.1;
- CGFloat alpha4 = 0.5;
- CGFloat locations[5] = {0,0.55,0.55,0.7,1};
-
- CGFloat components[20] = {1,1,1,alpha0,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3,1,1,1,alpha4};
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)5);
- CGColorSpaceRelease(colorSpace);
-
- // set shadow
- CGContextSetShadowWithColor(context, shadowOffset, shadowBlur, cgShadowColor);
-
- // set transparency layer and clip to mask
- CGContextBeginTransparencyLayer(context, NULL);
- CGContextClipToMask(context, imageRect, [self.image CGImage]);
-
- // fill and end the transparency layer
- CGContextSetFillColorWithColor(context, [self.selectedImageTintColor CGColor]);
- CGContextFillRect(context, imageRect);
- CGPoint start = CGPointMake(CGRectGetMidX(imageRect), imageRect.origin.y);
- CGPoint end = CGPointMake(CGRectGetMidX(imageRect)-imageRect.size.height/4, imageRect.size.height+imageRect.origin.y);
- CGContextDrawLinearGradient(context, colorGradient, end, start, 0);
- CGContextEndTransparencyLayer(context);
-
- CGGradientRelease(colorGradient);
+ if (self.selectedImage != nil) {
+ CGContextDrawImage(context, imageRect, self.selectedImage.CGImage);
+ }
+ else {
+ // default to shadow + gradient
+ // setup shadow
+ CGSize shadowOffset = CGSizeMake(0.0f, 1.0f);
+ CGFloat shadowBlur = 3.0;
+ CGColorRef cgShadowColor = [[UIColor blackColor] CGColor];
+
+ // setup gradient
+ CGFloat alpha0 = 0.8;
+ CGFloat alpha1 = 0.6;
+ CGFloat alpha2 = 0.0;
+ CGFloat alpha3 = 0.1;
+ CGFloat alpha4 = 0.5;
+ CGFloat locations[5] = {0,0.55,0.55,0.7,1};
+
+ CGFloat components[20] = {1,1,1,alpha0,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3,1,1,1,alpha4};
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+ CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)5);
+ CGColorSpaceRelease(colorSpace);
+
+ // set shadow
+ CGContextSetShadowWithColor(context, shadowOffset, shadowBlur, cgShadowColor);
+
+ // set transparency layer and clip to mask
+ CGContextBeginTransparencyLayer(context, NULL);
+ CGContextClipToMask(context, imageRect, [self.image CGImage]);
+
+ // fill and end the transparency layer
+ CGContextSetFillColorWithColor(context, [self.selectedImageTintColor CGColor]);
+ CGContextFillRect(context, imageRect);
+ CGPoint start = CGPointMake(CGRectGetMidX(imageRect), imageRect.origin.y);
+ CGPoint end = CGPointMake(CGRectGetMidX(imageRect)-imageRect.size.height/4, imageRect.size.height+imageRect.origin.y);
+ CGContextDrawLinearGradient(context, colorGradient, end, start, 0);
+ CGContextEndTransparencyLayer(context);
+
+ CGGradientRelease(colorGradient);
+ }
} else {
CGContextDrawImage(context, imageRect, self.image.CGImage);
}

0 comments on commit c228a81

Please sign in to comment.
Something went wrong with that request. Please try again.