Permalink
Browse files

Draw item highlight in NGTabBar

  • Loading branch information...
1 parent 5971277 commit 94505db73b314df5bf8211c9e0f2bc654d483989 @myell0w myell0w committed Apr 26, 2012
@@ -29,6 +29,10 @@ typedef enum {
@property (nonatomic, strong) UIColor *tintColor;
/** defaults to nil */
@property (nonatomic, strong) UIImage *backgroundImage;
+/** flag whether the semi-transparent item highlight is shown */
+@property (nonatomic, assign) BOOL showsItemHighlight;
+/** defaults to white */
+@property (nonatomic, strong) UIColor *itemHighlightColor;
- (void)selectItemAtIndex:(NSUInteger)index;
- (void)deselectSelectedItem;
@@ -3,6 +3,7 @@
#define kNGDefaultTintColor [UIColor blackColor]
+#define kNGDefaultItemHighlightColor [UIColor colorWithWhite:1.f alpha:0.2f]
@interface NGTabBar () {
@@ -27,6 +28,8 @@ @implementation NGTabBar
@synthesize tintColor = _tintColor;
@synthesize backgroundImage = _backgroundImage;
@synthesize backgroundView = _backgroundView;
+@synthesize showsItemHighlight = _showsItemHighlight;
+@synthesize itemHighlightColor = _itemHighlightColor;
////////////////////////////////////////////////////////////////////////
#pragma mark - Lifecycle
@@ -43,6 +46,7 @@ - (id)initWithFrame:(CGRect)frame {
_layoutStrategy = NGTabBarLayoutStrategyStrungTogether;
_itemPadding = 0.f;
_position = kNGTabBarPositionDefault;
+ _showsItemHighlight = YES;
[self createGradient];
}
@@ -123,14 +127,24 @@ - (void)layoutSubviews {
currentFrameLeft += appliedItemPadding;
}
}
+
+ // re-compute content size
+ NGTabBarItem *lastItem = [self.items lastObject];
+
+ if (NGTabBarIsVertical(self.position)) {
+ self.contentSize = CGSizeMake(lastItem.frame.size.width, lastItem.frame.origin.y + lastItem.frame.size.height);
+ } else {
+ self.contentSize = CGSizeMake(lastItem.frame.origin.x + lastItem.frame.size.width, lastItem.frame.size.height);
+ }
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
+ CGContextRef context = UIGraphicsGetCurrentContext();
+
if (self.backgroundImage == nil) {
CGRect bounds = self.bounds;
- CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint start;
CGPoint end;
@@ -155,6 +169,27 @@ - (void)drawRect:(CGRect)rect {
CGContextDrawLinearGradient(context, _gradientRef, start, end, 0);
CGContextRestoreGState(context);
}
+
+ if (self.showsItemHighlight) {
+ CGRect itemRect = [[self.items objectAtIndex:self.selectedItemIndex] frame];
+ CGRect highlightRect = NGTabBarIsVertical(self.position) ? CGRectInset(itemRect, 2.f, 0.f) : CGRectInset(itemRect, 0.f, 2.f);
+ CGFloat cornerRadius = 5.f;
+ CGPoint min = CGPointMake(CGRectGetMinX(highlightRect), CGRectGetMinY(highlightRect));
+ CGPoint mid = CGPointMake(CGRectGetMidX(highlightRect), CGRectGetMidY(highlightRect));
+ CGPoint max = CGPointMake(CGRectGetMaxX(highlightRect), CGRectGetMaxY(highlightRect));
+
+ CGContextSaveGState(context);
+ CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:1.f alpha:0.2f].CGColor);
+ CGContextMoveToPoint(context, min.x, mid.y);
+ CGContextAddArcToPoint(context, min.x, min.y, mid.x, min.y, cornerRadius);
+ CGContextAddArcToPoint(context, max.x, min.y, max.x, mid.y, cornerRadius);
+ CGContextAddArcToPoint(context, max.x, max.y, mid.x, max.y, cornerRadius);
+ CGContextAddArcToPoint(context, min.x, max.y, min.x, mid.y, cornerRadius);
+
+ CGContextClosePath(context);
+ CGContextFillPath(context);
+ CGContextRestoreGState(context);
+ }
}
////////////////////////////////////////////////////////////////////////
@@ -185,8 +220,6 @@ - (void)setPosition:(NGTabBarPosition)position {
self.alwaysBounceVertical = NO;
}
- // TODO: re-compute contentSize
-
[self setNeedsLayout];
[self setNeedsDisplay];
}
@@ -200,6 +233,7 @@ - (void)selectItemAtIndex:(NSUInteger)index {
item.selected = YES;
self.selectedItemIndex = index;
+ [self setNeedsDisplay];
}
}
@@ -209,6 +243,7 @@ - (void)deselectSelectedItem {
selectedItem.selected = NO;
self.selectedItemIndex = NSNotFound;
+ [self setNeedsDisplay];
}
}
@@ -244,6 +279,24 @@ - (void)setTintColor:(UIColor *)tintColor {
}
}
+- (UIColor *)itemHighlightColor {
+ return _itemHighlightColor ?: kNGDefaultItemHighlightColor;
+}
+
+- (void)setItemHighlightColor:(UIColor *)itemHighlightColor {
+ if (itemHighlightColor != _itemHighlightColor) {
+ _itemHighlightColor = itemHighlightColor;
+ [self setNeedsDisplay];
+ }
+}
+
+- (void)setShowsItemHighlight:(BOOL)showsItemHighlight {
+ if (showsItemHighlight != _showsItemHighlight) {
+ _showsItemHighlight = showsItemHighlight;
+ [self setNeedsDisplay];
+ }
+}
+
////////////////////////////////////////////////////////////////////////
#pragma mark - Private
////////////////////////////////////////////////////////////////////////
@@ -39,12 +39,13 @@
@property (nonatomic, assign) BOOL tabBarHidden;
-- (void)setTabBarHidden:(BOOL)tabBarHidden animated:(BOOL)animated;
+
+/** The designated initializer. */
+- (id)initWithDelegate:(id<NGTabBarControllerDelegate>)delegate;
/** Sets the view controllers of the tab bar controller. */
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated;
-/** The designated initializer. */
-- (id)initWithDelegate:(id<NGTabBarControllerDelegate>)delegate;
+- (void)setTabBarHidden:(BOOL)tabBarHidden animated:(BOOL)animated;
@end
@@ -131,27 +131,6 @@ - (void)drawRect:(CGRect)rect {
CGContextRestoreGState(context);
}
-
- // draw highlight
- if (_selectedByUser) {
- CGRect highlightRect = CGRectInset(rect, 2.f, 2.f);
- CGFloat cornerRadius = 4.f;
- CGPoint min = CGPointMake(CGRectGetMinX(highlightRect), CGRectGetMinY(highlightRect));
- CGPoint mid = CGPointMake(CGRectGetMidX(highlightRect), CGRectGetMidY(highlightRect));
- CGPoint max = CGPointMake(CGRectGetMaxX(highlightRect), CGRectGetMaxY(highlightRect));
-
- CGContextSaveGState(context);
- CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:1.f alpha:0.2f].CGColor);
- CGContextMoveToPoint(context, min.x, mid.y);
- CGContextAddArcToPoint(context, min.x, min.y, mid.x, min.y, cornerRadius);
- CGContextAddArcToPoint(context, max.x, min.y, max.x, mid.y, cornerRadius);
- CGContextAddArcToPoint(context, max.x, max.y, mid.x, max.y, cornerRadius);
- CGContextAddArcToPoint(context, min.x, max.y, min.x, mid.y, cornerRadius);
-
- CGContextClosePath(context);
- CGContextFillPath(context);
- CGContextRestoreGState(context);
- }
}
////////////////////////////////////////////////////////////////////////

0 comments on commit 94505db

Please sign in to comment.