Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed item highlight not moving with scrollView

  • Loading branch information...
commit 9a8eb145a6c82f4ee340082c896d474892768d7e 1 parent 94505db
@myell0w myell0w authored
Showing with 29 additions and 25 deletions.
  1. +29 −25 NGTabBarController/NGTabBar.m
View
54 NGTabBarController/NGTabBar.m
@@ -1,5 +1,6 @@
#import "NGTabBar.h"
#import "NGTabBarItem.h"
+#import <QuartzCore/QuartzCore.h>
#define kNGDefaultTintColor [UIColor blackColor]
@@ -11,8 +12,10 @@ @interface NGTabBar () {
}
@property (nonatomic, strong) UIView *backgroundView;
+@property (nonatomic, strong) UIView *itemHighlightView;
- (void)createGradient;
+- (void)updateItemHighlight;
- (CGFloat)dimensionToBeConsideredOfItem:(NGTabBarItem *)item;
@end
@@ -28,6 +31,7 @@ @implementation NGTabBar
@synthesize tintColor = _tintColor;
@synthesize backgroundImage = _backgroundImage;
@synthesize backgroundView = _backgroundView;
+@synthesize itemHighlightView = _itemHighlightView;
@synthesize showsItemHighlight = _showsItemHighlight;
@synthesize itemHighlightColor = _itemHighlightColor;
@@ -49,6 +53,7 @@ - (id)initWithFrame:(CGRect)frame {
_showsItemHighlight = YES;
[self createGradient];
+ [self updateItemHighlight];
}
return self;
@@ -136,6 +141,8 @@ - (void)layoutSubviews {
} else {
self.contentSize = CGSizeMake(lastItem.frame.origin.x + lastItem.frame.size.width, lastItem.frame.size.height);
}
+
+ [self updateItemHighlight];
}
- (void)drawRect:(CGRect)rect {
@@ -169,27 +176,6 @@ - (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);
- }
}
////////////////////////////////////////////////////////////////////////
@@ -233,7 +219,7 @@ - (void)selectItemAtIndex:(NSUInteger)index {
item.selected = YES;
self.selectedItemIndex = index;
- [self setNeedsDisplay];
+ [self updateItemHighlight];
}
}
@@ -243,7 +229,7 @@ - (void)deselectSelectedItem {
selectedItem.selected = NO;
self.selectedItemIndex = NSNotFound;
- [self setNeedsDisplay];
+ [self updateItemHighlight];
}
}
@@ -286,14 +272,14 @@ - (UIColor *)itemHighlightColor {
- (void)setItemHighlightColor:(UIColor *)itemHighlightColor {
if (itemHighlightColor != _itemHighlightColor) {
_itemHighlightColor = itemHighlightColor;
- [self setNeedsDisplay];
+ [self updateItemHighlight];
}
}
- (void)setShowsItemHighlight:(BOOL)showsItemHighlight {
if (showsItemHighlight != _showsItemHighlight) {
_showsItemHighlight = showsItemHighlight;
- [self setNeedsDisplay];
+ [self updateItemHighlight];
}
}
@@ -353,4 +339,22 @@ - (void)createGradient {
}
}
+- (void)updateItemHighlight {
+ if (self.selectedItemIndex != NSNotFound) {
+ CGRect itemRect = [[self.items objectAtIndex:self.selectedItemIndex] frame];
+
+ if (_itemHighlightView == nil) {
+ self.itemHighlightView = [[UIView alloc] initWithFrame:CGRectZero];
+ self.itemHighlightView.layer.cornerRadius = 5.f;
+ [self addSubview:self.itemHighlightView];
+ }
+
+ self.itemHighlightView.backgroundColor = self.itemHighlightColor;
+ self.itemHighlightView.frame = NGTabBarIsVertical(self.position) ? CGRectInset(itemRect, 2.f, 0.f) : CGRectInset(itemRect, 0.f, 2.f);
+ self.itemHighlightView.hidden = !self.showsItemHighlight;
+ } else {
+ self.itemHighlightView.hidden = YES;
+ }
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.