Skip to content
This repository
Browse code

Merge pull request #12 from phillbaker/selectedimage

Add selected image to tab bar item.
  • Loading branch information...
commit c228a81503868f36e165c89d86d2cda504dd83fb 2 parents ed7c287 + f0ae69f
Matthias Tretter myell0w authored
1  NGTabBarController/NGTabBarItem.h
@@ -11,6 +11,7 @@
11 11 @interface NGTabBarItem : UIControl
12 12
13 13 @property (nonatomic, strong) UIImage *image;
  14 +@property (nonatomic, strong) UIImage *selectedImage;
14 15 @property (nonatomic, copy) NSString *title;
15 16
16 17 @property (nonatomic, strong) UIColor *selectedImageTintColor;
75 NGTabBarController/NGTabBarItem.m
@@ -22,6 +22,7 @@ @implementation NGTabBarItem
22 22 @synthesize titleColor = _titleColor;
23 23 @synthesize selectedTitleColor = _selectedTitleColor;
24 24 @synthesize image = _image;
  25 +@synthesize selectedImage = _selectedImage;
25 26 @synthesize titleLabel = _titleLabel;
26 27
27 28 ////////////////////////////////////////////////////////////////////////
@@ -93,40 +94,46 @@ - (void)drawRect:(CGRect)rect {
93 94
94 95 // draw either a selection gradient/glow or a regular image
95 96 if (_selectedByUser) {
96   - // setup shadow
97   - CGSize shadowOffset = CGSizeMake(0.0f, 1.0f);
98   - CGFloat shadowBlur = 3.0;
99   - CGColorRef cgShadowColor = [[UIColor blackColor] CGColor];
100   -
101   - // setup gradient
102   - CGFloat alpha0 = 0.8;
103   - CGFloat alpha1 = 0.6;
104   - CGFloat alpha2 = 0.0;
105   - CGFloat alpha3 = 0.1;
106   - CGFloat alpha4 = 0.5;
107   - CGFloat locations[5] = {0,0.55,0.55,0.7,1};
108   -
109   - CGFloat components[20] = {1,1,1,alpha0,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3,1,1,1,alpha4};
110   - CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
111   - CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)5);
112   - CGColorSpaceRelease(colorSpace);
113   -
114   - // set shadow
115   - CGContextSetShadowWithColor(context, shadowOffset, shadowBlur, cgShadowColor);
116   -
117   - // set transparency layer and clip to mask
118   - CGContextBeginTransparencyLayer(context, NULL);
119   - CGContextClipToMask(context, imageRect, [self.image CGImage]);
120   -
121   - // fill and end the transparency layer
122   - CGContextSetFillColorWithColor(context, [self.selectedImageTintColor CGColor]);
123   - CGContextFillRect(context, imageRect);
124   - CGPoint start = CGPointMake(CGRectGetMidX(imageRect), imageRect.origin.y);
125   - CGPoint end = CGPointMake(CGRectGetMidX(imageRect)-imageRect.size.height/4, imageRect.size.height+imageRect.origin.y);
126   - CGContextDrawLinearGradient(context, colorGradient, end, start, 0);
127   - CGContextEndTransparencyLayer(context);
128   -
129   - CGGradientRelease(colorGradient);
  97 + if (self.selectedImage != nil) {
  98 + CGContextDrawImage(context, imageRect, self.selectedImage.CGImage);
  99 + }
  100 + else {
  101 + // default to shadow + gradient
  102 + // setup shadow
  103 + CGSize shadowOffset = CGSizeMake(0.0f, 1.0f);
  104 + CGFloat shadowBlur = 3.0;
  105 + CGColorRef cgShadowColor = [[UIColor blackColor] CGColor];
  106 +
  107 + // setup gradient
  108 + CGFloat alpha0 = 0.8;
  109 + CGFloat alpha1 = 0.6;
  110 + CGFloat alpha2 = 0.0;
  111 + CGFloat alpha3 = 0.1;
  112 + CGFloat alpha4 = 0.5;
  113 + CGFloat locations[5] = {0,0.55,0.55,0.7,1};
  114 +
  115 + CGFloat components[20] = {1,1,1,alpha0,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3,1,1,1,alpha4};
  116 + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  117 + CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)5);
  118 + CGColorSpaceRelease(colorSpace);
  119 +
  120 + // set shadow
  121 + CGContextSetShadowWithColor(context, shadowOffset, shadowBlur, cgShadowColor);
  122 +
  123 + // set transparency layer and clip to mask
  124 + CGContextBeginTransparencyLayer(context, NULL);
  125 + CGContextClipToMask(context, imageRect, [self.image CGImage]);
  126 +
  127 + // fill and end the transparency layer
  128 + CGContextSetFillColorWithColor(context, [self.selectedImageTintColor CGColor]);
  129 + CGContextFillRect(context, imageRect);
  130 + CGPoint start = CGPointMake(CGRectGetMidX(imageRect), imageRect.origin.y);
  131 + CGPoint end = CGPointMake(CGRectGetMidX(imageRect)-imageRect.size.height/4, imageRect.size.height+imageRect.origin.y);
  132 + CGContextDrawLinearGradient(context, colorGradient, end, start, 0);
  133 + CGContextEndTransparencyLayer(context);
  134 +
  135 + CGGradientRelease(colorGradient);
  136 + }
130 137 } else {
131 138 CGContextDrawImage(context, imageRect, self.image.CGImage);
132 139 }

0 comments on commit c228a81

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