diff --git a/Classes/VSTabBar/UIImage+ImageProcessing.h b/Classes/VSTabBar/UIImage+ImageProcessing.h new file mode 100755 index 0000000..3c6b737 --- /dev/null +++ b/Classes/VSTabBar/UIImage+ImageProcessing.h @@ -0,0 +1,13 @@ +// +// UIImage+ImageProcessing.h +// +// Created by Vincent Saluzzo on 28/05/12. +// Copyright (c) 2012. All rights reserved. +// + +#import + +@interface UIImage (ImageProcessing) +- (UIImage *) toGrayscale; +- (UIImage *) tintWithColor:(UIColor *)tintColor; +@end diff --git a/Classes/VSTabBar/UIImage+ImageProcessing.m b/Classes/VSTabBar/UIImage+ImageProcessing.m new file mode 100755 index 0000000..26a5f1b --- /dev/null +++ b/Classes/VSTabBar/UIImage+ImageProcessing.m @@ -0,0 +1,126 @@ +// +// UIImage+ImageProcessing.m +// +// Created by Vincent Saluzzo on 28/05/12. +// Copyright (c) 2012. All rights reserved. +// + +#import "UIImage+ImageProcessing.h" + +@implementation UIImage (ImageProcessing) + +- (UIImage *) toGrayscale +{ + const int RED = 1; + const int GREEN = 2; + const int BLUE = 3; + + // Create image rectangle with current image width/height + CGRect imageRect = CGRectMake(0, 0, self.size.width * self.scale, self.size.height * self.scale); + + int width = imageRect.size.width; + int height = imageRect.size.height; + + // the pixels will be painted to this array + uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t)); + + // clear the pixels so any transparency is preserved + memset(pixels, 0, width * height * sizeof(uint32_t)); + + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + + // create a context with RGBA pixels + CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, + kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast); + + // paint the bitmap to our context which will fill in the pixels array + CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self CGImage]); + + for(int y = 0; y < height; y++) { + for(int x = 0; x < width; x++) { + uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x]; + + // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale + uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE]; + + // set the pixels to gray + rgbaPixel[RED] = gray; + rgbaPixel[GREEN] = gray; + rgbaPixel[BLUE] = gray; + } + } + + // create a new CGImageRef from our context with the modified pixels + CGImageRef image = CGBitmapContextCreateImage(context); + + // we're done with the context, color space, and pixels + CGContextRelease(context); + CGColorSpaceRelease(colorSpace); + free(pixels); + + // make a new UIImage to return + UIImage *resultUIImage = [UIImage imageWithCGImage:image + scale:self.scale + orientation:UIImageOrientationUp]; + + // we're done with image now too + CGImageRelease(image); + + return resultUIImage; +} + +- (UIImage *)tintWithColor:(UIColor *)tintColor +{ + // Begin drawing + CGRect aRect = CGRectMake(0.f, 0.f, self.size.width, self.size.height); + CGImageRef alphaMask; + + // + // Compute mask flipping image + // + { + UIGraphicsBeginImageContext(aRect.size); + CGContextRef c = UIGraphicsGetCurrentContext(); + + // draw image + CGContextTranslateCTM(c, 0, aRect.size.height); + CGContextScaleCTM(c, 1.0, -1.0); + [self drawInRect: aRect]; + + alphaMask = CGBitmapContextCreateImage(c); + + UIGraphicsEndImageContext(); + } + + // + UIGraphicsBeginImageContext(aRect.size); + + // Get the graphic context + CGContextRef c = UIGraphicsGetCurrentContext(); + + // Draw the image + [self drawInRect:aRect]; + + // Mask + CGContextClipToMask(c, aRect, alphaMask); + + // Set the fill color space + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + CGContextSetFillColorSpace(c, colorSpace); + + // Set the fill color + CGContextSetFillColorWithColor(c, tintColor.CGColor); + + UIRectFillUsingBlendMode(aRect, kCGBlendModeNormal); + + UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + // Release memory + CGColorSpaceRelease(colorSpace); + CGImageRelease(alphaMask); + + return img; +} + +@end diff --git a/Classes/VSTabBar/VSTabBar.h b/Classes/VSTabBar/VSTabBar.h new file mode 100755 index 0000000..c7420e5 --- /dev/null +++ b/Classes/VSTabBar/VSTabBar.h @@ -0,0 +1,44 @@ +// +// VSTabBar.h +// +// Created by Vincent Saluzzo on 25/05/12. +// Copyright (c) 2012. All rights reserved. +// + +#import +#import "VSTabBarItem.h" +#import "UIImage+ImageProcessing.h" + +@class VSTabBar; + +@protocol VSTabBarDelegate + +-(void) tabBar:(VSTabBar*)tabBar selectedItemWithIndex:(NSInteger)index; + +@end + + +@interface VSTabBar : UIView { + + +} + +@property (nonatomic, readonly) NSArray *itemList; +@property (nonatomic, readonly) VSTabBarItem *selectedItem; + +@property (nonatomic, assign) BOOL drawTitle; +@property (nonatomic, assign) BOOL drawImage; +@property (nonatomic, assign) BOOL showCurrentSelected; +@property (nonatomic, assign) BOOL showSeparationBetweenItems; +@property (nonatomic, assign) BOOL showSelectedItem; +@property (nonatomic, assign) BOOL isTopBar; +@property (nonatomic, retain) UIColor* backgroundColor; +@property (nonatomic, retain) UIColor* selectionGradientColor; +@property (nonatomic, retain) UIColor* currentSelectionIndicatorColor; +@property (nonatomic, retain) UIColor* separatorColor; +@property (nonatomic, retain) UIColor* foregroundColor; +@property (nonatomic, assign) id delegate; + +-(void) setItems:(NSArray*)items; +-(void) selectItem:(NSInteger)index; +@end diff --git a/Classes/VSTabBar/VSTabBar.m b/Classes/VSTabBar/VSTabBar.m new file mode 100755 index 0000000..825fed0 --- /dev/null +++ b/Classes/VSTabBar/VSTabBar.m @@ -0,0 +1,294 @@ +// +// VSTabBar.m +// +// Created by Vincent Saluzzo on 25/05/12. +// Copyright (c) 2012. All rights reserved. +// + +#import "VSTabBar.h" + + +@interface VSTabBar() +@property (nonatomic,retain) NSArray* _items; +@property (nonatomic) NSInteger _selectedItem; +@property (nonatomic,retain) NSMutableArray* _itemsView; +@property (nonatomic) NSInteger _currentItemViewed; + +@end + +@implementation VSTabBar +@synthesize drawTitle = _drawTitle; +@synthesize drawImage = _drawImage; +@synthesize showSeparationBetweenItems = _showSeparationBetweenItems; +@synthesize showSelectedItem = _showSelectedItem; +@synthesize showCurrentSelected = _showCurrentSelected; +@synthesize isTopBar = _isTopBar; +@synthesize backgroundColor = _backgroundColor; +@synthesize separatorColor = _separatorColor; +@synthesize selectionGradientColor = _selectionGradientColor; +@synthesize currentSelectionIndicatorColor = _currentSelectionIndicatorColor; +@synthesize foregroundColor = _foregroundColor; +@synthesize delegate; + +@synthesize _items; +@synthesize _selectedItem; +@synthesize _itemsView; +@synthesize _currentItemViewed; + + +@synthesize itemList; +@synthesize selectedItem; + + +- (id)initWithCoder:(NSCoder *)coder +{ + self = [super initWithCoder:coder]; + if (self) { + [self initTabBar]; + } + return self; +} + +- (id)initWithFrame:(CGRect)frame +{ + self = [super initWithFrame:frame]; + if (self) { + [self initTabBar]; + } + return self; +} + +-(NSArray*) itemList +{ + return self._items; +} + +-(VSTabBarItem*) selectedItem +{ + VSTabBarItem *result = nil; + if (self._items.count > 0 && self._selectedItem > -1) { + result = [self._items objectAtIndex:self._selectedItem]; + } + return result; +} + +-(void) initTabBar { + self._selectedItem = -1; + self._currentItemViewed = -1; + self._itemsView = [[NSMutableArray alloc] init]; + self.drawImage = YES; + self.drawTitle = YES; + self.isTopBar = NO; + self.showSelectedItem = YES; + self.showCurrentSelected = YES; + self.showSeparationBetweenItems = YES; + self.currentSelectionIndicatorColor = [UIColor whiteColor]; + self.backgroundColor = [UIColor blackColor]; + self.selectionGradientColor = [UIColor whiteColor]; + self.separatorColor = [UIColor whiteColor]; + self.foregroundColor = [UIColor whiteColor]; + + self.userInteractionEnabled = YES; +} + +// Only override drawRect: if you perform custom drawing. +// An empty implementation adversely affects performance during animation. +- (void)drawRect:(CGRect)rect +{ + CGContextRef currentContext = UIGraphicsGetCurrentContext(); + CGMutablePathRef path = CGPathCreateMutable(); + + CGPathMoveToPoint(path, NULL, 0, 0); + CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)); + CGContextAddPath(currentContext, path); + + [_backgroundColor setFill]; + CGContextDrawPath(currentContext, kCGPathFill); + + for (UIView* subview in self.subviews) { + [subview removeFromSuperview]; + [subview release]; + } + + if(self._items) { + int count = [self._items count]; + float itemWidth = self.frame.size.width / count; + float itemHeight = self.frame.size.height; + + [self._itemsView removeAllObjects]; + int indexOfCreation = 0; + for (VSTabBarItem* anItem in self._items) { + UIView* itemView = [[UIView alloc] initWithFrame:CGRectMake(itemWidth*indexOfCreation, 0, itemWidth, itemHeight)]; + + if(_drawImage &&_drawTitle) { + UIImageView* itemImage = [[UIImageView alloc] initWithImage:[anItem.image tintWithColor:_foregroundColor]]; + UILabel* itemLabel = [[UILabel alloc] init]; + if(_isTopBar) { + itemImage.frame = CGRectMake(0*indexOfCreation+10, 5, itemWidth-20, itemHeight/10*5 -5); + itemLabel.frame = CGRectMake(10, 5 + itemHeight/10*5, itemWidth-20, itemHeight/10*5 - 15); + } else { + itemImage.frame = CGRectMake(0*indexOfCreation+10, 10, itemWidth-20, itemHeight/10*5 -5); + itemLabel.frame = CGRectMake(10, 5 + itemHeight/10*5, itemWidth-20, itemHeight/10*5 - 5); + } + [itemImage setContentMode:UIViewContentModeScaleAspectFit]; + + + [itemLabel setText:anItem.title]; + [itemLabel setContentMode:UIViewContentModeScaleAspectFit]; + [itemLabel setFont:[UIFont systemFontOfSize:12.0f]]; + [itemLabel setBackgroundColor:[UIColor clearColor]]; + [itemLabel setTextColor:_foregroundColor]; + + [itemView addSubview:itemLabel]; + [itemView addSubview:itemImage]; + } else if(_drawImage && !_drawTitle) { + UIImageView* itemImage = [[UIImageView alloc] initWithImage:anItem.image]; + if(_isTopBar) { + itemImage.frame = CGRectMake(0*indexOfCreation+10, 5, itemWidth-20, itemHeight - 15); + } else { + itemImage.frame = CGRectMake(0*indexOfCreation+10, 10, itemWidth-20, itemHeight - 15); + } + [itemImage setContentMode:UIViewContentModeScaleAspectFit]; + [itemView addSubview:itemImage]; + + } else if(!_drawImage && _drawTitle) { + UILabel* itemLabel = [[UILabel alloc] init]; + itemLabel.frame = CGRectMake(10, 5, itemWidth-20, itemHeight - 5); + [itemLabel setText:anItem.title]; + [itemLabel setContentMode:UIViewContentModeScaleAspectFit]; + [itemLabel setFont:[UIFont systemFontOfSize:12.0f]]; + [itemLabel setBackgroundColor:[UIColor clearColor]]; + [itemLabel setTextColor:_foregroundColor]; + + [itemView addSubview:itemLabel]; + + } else { + + } + [self addSubview:itemView]; + [self._itemsView addObject:itemView]; + indexOfCreation++; + } + } + + if(self._selectedItem > -1 && _showSelectedItem) { + float itemWidth = self.frame.size.width / [self._items count]; + float xPoint = _selectedItem * itemWidth + itemWidth/2; + + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + + UIColor* startColor = _selectionGradientColor; + CGFloat* startColorComponents = (CGFloat*)CGColorGetComponents([startColor CGColor]); + + UIColor* endColor = _backgroundColor; + CGFloat* endColorComponents = (CGFloat*)CGColorGetComponents([endColor CGColor]); + + CGFloat colorComponents[8] = { + //Four components of the blue color + startColorComponents[0], + startColorComponents[1], + startColorComponents[2], + startColorComponents[3], + + //Four components of the green color + endColorComponents[0], + endColorComponents[1], + endColorComponents[2], + endColorComponents[3] + }; + + CGFloat colorIndices[2] = { + 0.0f, 1.0f + }; + + CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, &colorComponents, &colorIndices, 2); + + CGColorSpaceRelease(colorSpace); + + float centerOfRadial = (_isTopBar) ? 0.0f : self.frame.size.height; + + CGContextDrawRadialGradient(currentContext, gradient, CGPointMake(xPoint, centerOfRadial), 0.0f, CGPointMake(xPoint, centerOfRadial), itemWidth/2.0f, kCGGradientDrawsAfterEndLocation|kCGGradientDrawsBeforeStartLocation); + + CGGradientRelease(gradient); + + CGPathRelease(path); + } + + if(self._currentItemViewed > -1 && _showCurrentSelected) { + float itemWidth = self.frame.size.width / [self._items count]; + CGMutablePathRef pathCurrentItemSelected = CGPathCreateMutable(); + if(_isTopBar) { + float topArrow = _currentItemViewed * itemWidth + itemWidth/2; + CGPathMoveToPoint(pathCurrentItemSelected, NULL, topArrow, self.frame.size.height-7); + CGPathAddLineToPoint(pathCurrentItemSelected, NULL, topArrow-5, self.frame.size.height); + CGPathAddLineToPoint(pathCurrentItemSelected, NULL, topArrow+5, self.frame.size.height); + } else { + float topArrow = _currentItemViewed * itemWidth + itemWidth/2; + CGPathMoveToPoint(pathCurrentItemSelected, NULL, topArrow, 7); + CGPathAddLineToPoint(pathCurrentItemSelected, NULL, topArrow-5, 0); + CGPathAddLineToPoint(pathCurrentItemSelected, NULL, topArrow+5, 0); + } + + [_currentSelectionIndicatorColor setFill]; + + CGContextAddPath(UIGraphicsGetCurrentContext(), pathCurrentItemSelected); + CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFill); + + CGPathRelease(pathCurrentItemSelected); + + } + + if(_showSeparationBetweenItems) { + float itemWidth = self.frame.size.width / [self._items count]; + for(int i = 0; i < [self._items count]; i++) { + [_separatorColor set]; + CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 0.1f); + CGContextMoveToPoint(UIGraphicsGetCurrentContext(), itemWidth*(float)i, 0); + CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), itemWidth*(float)i, self.frame.size.height); + CGContextStrokePath(UIGraphicsGetCurrentContext()); + } + } +} + + +-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + if([touches count] == 1) { + UITouch* aTouch = [[touches allObjects] objectAtIndex:0]; + float itemWidth = self.frame.size.width / [self._items count]; + CGPoint touchPoint = [aTouch locationInView:self]; + + NSInteger itemTouched = touchPoint.x / itemWidth; + + self._selectedItem = itemTouched; + self._currentItemViewed = _selectedItem; + + } +} + +-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { + [super touchesCancelled:touches withEvent:event]; +} + +-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + _selectedItem = -1; + [self setNeedsDisplay]; + if([delegate respondsToSelector:@selector(tabBar:selectedItemWithIndex:)]) { + [delegate tabBar:self selectedItemWithIndex:_currentItemViewed]; + } +} + +-(void)setItems:(NSArray *)itemsArray { + if(self._items) { + [self._items release]; + } + self._items = [itemsArray retain]; + [self setNeedsDisplay]; +} + +-(void) selectItem:(NSInteger)index { + _selectedItem = index; + _currentItemViewed = _selectedItem; + [self setNeedsDisplay]; +} + +@end diff --git a/Classes/VSTabBar/VSTabBarItem.h b/Classes/VSTabBar/VSTabBarItem.h new file mode 100755 index 0000000..b00bb54 --- /dev/null +++ b/Classes/VSTabBar/VSTabBarItem.h @@ -0,0 +1,22 @@ +// +// VSTabBarItem.h +// +// Created by Vincent Saluzzo on 25/05/12. +// Copyright (c) 2012. All rights reserved. +// + +#import + +@interface VSTabBarItem : NSObject { + UIImage* _image; + NSString* _title; +} +@property(nonatomic,retain) UIImage* image; +@property(nonatomic,retain) NSString* title; +@property (nonatomic) NSInteger tag; + +-(id) initWithImage:(UIImage*)anImage andTitle:(NSString*)aTitle andTag:(NSInteger)itemTag; +-(id) initWithImage:(UIImage*)anImage andTag:(NSInteger)itemTag; +-(id) initWithTitle:(NSString*)aTitle andTag:(NSInteger)itemTag; + +@end diff --git a/Classes/VSTabBar/VSTabBarItem.m b/Classes/VSTabBar/VSTabBarItem.m new file mode 100755 index 0000000..d634be7 --- /dev/null +++ b/Classes/VSTabBar/VSTabBarItem.m @@ -0,0 +1,32 @@ +// +// VSTabBarItem.m +// +// Created by Vincent Saluzzo on 25/05/12. +// Copyright (c) 2012. All rights reserved. +// + +#import "VSTabBarItem.h" + +@implementation VSTabBarItem +@synthesize image; +@synthesize title; +@synthesize tag; + +-(id)initWithImage:(UIImage *)anImage andTitle:(NSString *)aTitle andTag:(NSInteger)itemTag { + self = [super init]; + if(self) { + self.image = anImage; + self.title = aTitle; + self.tag = itemTag; + } + return self; +} + +-(id)initWithImage:(UIImage *)anImage andTag:(NSInteger)itemTag { + return [self initWithImage:anImage andTitle:nil andTag:itemTag]; +} + +-(id)initWithTitle:(NSString *)aTitle andTag:(NSInteger)itemTag { + return [self initWithImage:nil andTitle:aTitle andTag:itemTag]; +} +@end diff --git a/Classes/ViewController.h b/Classes/ViewController.h old mode 100644 new mode 100755 diff --git a/Classes/ViewController.m b/Classes/ViewController.m old mode 100644 new mode 100755 index 7a07021..346765c --- a/Classes/ViewController.m +++ b/Classes/ViewController.m @@ -4,6 +4,7 @@ // #import "ViewController.h" +#import "VSTabBar.h" @implementation ViewController @@ -17,6 +18,8 @@ - (void)viewDidLoad { [super viewDidLoad]; // Items + + /* UITabBarItem *favorites = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0]; UITabBarItem *topRated = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:1]; UITabBarItem *featured = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:2]; @@ -28,31 +31,86 @@ - (void)viewDidLoad { UITabBarItem *downloads = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:8]; downloads.badgeValue = @"2"; UITabBarItem *mostRecent = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostRecent tag:9]; UITabBarItem *mostViewed = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:10]; - - // Tab bar - self.tabBar = [[InfiniTabBar alloc] initWithItems:[NSArray arrayWithObjects:favorites, - topRated, - featured, - recents, - contacts, - history, - bookmarks, - search, - downloads, - mostRecent, - mostViewed, nil]]; - - [favorites release]; - [topRated release]; - [featured release]; - [recents release]; - [contacts release]; - [history release]; - [bookmarks release]; - [search release]; - [downloads release]; - [mostRecent release]; - [mostViewed release]; + UITabBarItem *mostPopular = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:11]; + UITabBarItem *veryPopular = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:12]; + UITabBarItem *nazarItem = [[UITabBarItem alloc] initWithTitle:nil image:[UIImage imageNamed:@"topbar_icon"] tag:13]; + */ + + + /* + UIColor *tabBGColor = [UIColor lightGrayColor]; + self.tabBar = [[InfiniTabBar alloc] initWithItems:[NSArray arrayWithObjects:favorites, + topRated, + featured, + recents, + contacts, + history, + bookmarks, + search, + downloads, + mostRecent, + mostViewed, + mostPopular, + veryPopular, + nazarItem, + nil] andBackgroundColor:tabBGColor]; + + + [favorites release]; + [topRated release]; + [featured release]; + [recents release]; + [contacts release]; + [history release]; + [bookmarks release]; + [search release]; + [downloads release]; + [mostRecent release]; + [mostViewed release]; + */ + + + VSTabBarItem *item1 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"nazar.png"] andTag:0]; + VSTabBarItem *item2 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"Beer.png"] andTag:1]; + VSTabBarItem *item3 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"Basket.png"] andTag:2]; + VSTabBarItem *item4 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"topbar_icon.png"] andTag:3]; + VSTabBarItem *item5 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"Money-Bag.png"] andTag:4]; + VSTabBarItem *item6 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"nazar.png"] andTag:5]; + VSTabBarItem *item7 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"Beer.png"] andTag:6]; + VSTabBarItem *item8 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"Basket.png"] andTag:7]; + VSTabBarItem *item9 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"topbar_icon.png"] andTag:8]; + VSTabBarItem *item10 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"Money-Bag.png"] andTag:9]; + VSTabBarItem *item11 = [[VSTabBarItem alloc] initWithImage:[UIImage imageNamed:@"nazar.png"] andTag:10]; + + + UIColor *tabBGColor = [UIColor lightGrayColor]; + self.tabBar = [[InfiniTabBar alloc] initWithItems:[NSArray arrayWithObjects:item1, + item2, + item3, + item4, + item5, + item6, + item7, + item8, + item9, + item10, + item11, + nil] andBackgroundColor:tabBGColor]; + + + [item1 release]; + [item2 release]; + [item3 release]; + [item4 release]; + [item5 release]; + [item6 release]; + [item7 release]; + [item8 release]; + [item9 release]; + [item10 release]; + [item11 release]; + + // Don't show scroll indicator self.tabBar.showsHorizontalScrollIndicator = NO; diff --git a/InfiniTabBar.h b/InfiniTabBar.h old mode 100644 new mode 100755 index 3be5e25..eed456b --- a/InfiniTabBar.h +++ b/InfiniTabBar.h @@ -1,27 +1,21 @@ // // InfiniTabBar.h // Created by http://github.com/iosdeveloper +// Edited by https://github.com/tolgatanriverdi // #import @protocol InfiniTabBarDelegate; -@interface InfiniTabBar : UIScrollView { - id infiniTabBarDelegate; - NSMutableArray *tabBars; - UITabBar *aTabBar; - UITabBar *bTabBar; -} +@interface InfiniTabBar : UIScrollView + @property (nonatomic, assign) id infiniTabBarDelegate; -@property (nonatomic, retain) NSMutableArray *tabBars; -@property (nonatomic, retain) UITabBar *aTabBar; -@property (nonatomic, retain) UITabBar *bTabBar; -- (id)initWithItems:(NSArray *)items; + +- (id)initWithItems:(NSArray *)items andBackgroundColor:(UIColor*)tabBackgroundColor; - (void)setBounces:(BOOL)bounces; -// Don't set more items than initially - (void)setItems:(NSArray *)items animated:(BOOL)animated; - (int)currentTabBarTag; - (int)selectedItemTag; diff --git a/InfiniTabBar.m b/InfiniTabBar.m old mode 100644 new mode 100755 index 065bdab..0e7466f --- a/InfiniTabBar.m +++ b/InfiniTabBar.m @@ -1,9 +1,17 @@ // // InfiniTabBar.m // Created by http://github.com/iosdeveloper +// Edited by https://github.com/tolgatanriverdi // #import "InfiniTabBar.h" +#import "VSTabBar.h" + +@interface InfiniTabBar() +@property (nonatomic, retain) NSMutableArray *tabBars; +@property (nonatomic, retain) VSTabBar *aTabBar; +@property (nonatomic, retain) VSTabBar *bTabBar; +@end @implementation InfiniTabBar @@ -12,11 +20,13 @@ @implementation InfiniTabBar @synthesize aTabBar; @synthesize bTabBar; -- (id)initWithItems:(NSArray *)items { - self = [super initWithFrame:CGRectMake(0.0, 411.0, 320.0, 49.0)]; - // TODO: - //self = [super initWithFrame:CGRectMake(self.superview.frame.origin.x + self.superview.frame.size.width - 320.0, self.superview.frame.origin.y + self.superview.frame.size.height - 49.0, 320.0, 49.0)]; - // Doesn't work. self is nil at this point. + +#define CONTENT_HEIGHT 39.0f +#define ITEMS_PER_PAGE 5.0f +#define TAB_BAR_WIDTH 320.0/ITEMS_PER_PAGE + +- (id)initWithItems:(NSArray *)items andBackgroundColor:(UIColor*)tabBackgroundColor { + self = [super initWithFrame:CGRectMake(0.0, 411.0, 320.0, CONTENT_HEIGHT)]; if (self) { self.pagingEnabled = YES; @@ -25,29 +35,46 @@ - (id)initWithItems:(NSArray *)items { self.tabBars = [[[NSMutableArray alloc] init] autorelease]; float x = 0.0; + //NSLog(@"%s PAGE COUNT: %f",__PRETTY_FUNCTION__,ceil(items.count / ITEMS_PER_PAGE) ); - for (double d = 0; d < ceil(items.count / 5.0); d ++) { - UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(x, 0.0, 320.0, 49.0)]; - tabBar.delegate = self; - + for (double d = 0; d < ceil(items.count / ITEMS_PER_PAGE); d ++) { int len = 0; - for (int i = d * 5; i < d * 5 + 5; i ++) - if (i < items.count) + for (int i = d * ITEMS_PER_PAGE; i < d * ITEMS_PER_PAGE + ITEMS_PER_PAGE; i ++) { + if (i < items.count) { len ++; + } + } + + /* + UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(x, 0.0, TAB_BAR_WIDTH*len, CONTENT_HEIGHT)]; + tabBar.backgroundColor = tabBackgroundColor; + tabBar.barTintColor = tabBackgroundColor; + tabBar.delegate = self; - tabBar.items = [items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(d * 5, len)]]; + tabBar.items = [items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(d * ITEMS_PER_PAGE, len)]]; + //NSLog(@"NEW TABBAR ITEM COUNT: %d",tabBar.items.count); + */ + + VSTabBar *tabBar = [[VSTabBar alloc] initWithFrame:CGRectMake(x, 0.0, TAB_BAR_WIDTH*len, CONTENT_HEIGHT)]; + tabBar.backgroundColor = tabBackgroundColor; + tabBar.delegate = self; + tabBar.drawImage = YES; + tabBar.drawTitle = NO; + + [tabBar setItems:[items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(d * ITEMS_PER_PAGE, len)]]]; + [self addSubview:tabBar]; [self.tabBars addObject:tabBar]; - [tabBar release]; - x += 320.0; + x += TAB_BAR_WIDTH*tabBar.itemList.count; + [tabBar release]; } - self.contentSize = CGSizeMake(x, 49.0); + self.contentSize = CGSizeMake(x, CONTENT_HEIGHT); } return self; @@ -58,13 +85,15 @@ - (void)setBounces:(BOOL)bounces { int count = self.tabBars.count; if (count > 0) { - if (self.aTabBar == nil) - self.aTabBar = [[[UITabBar alloc] initWithFrame:CGRectMake(-320.0, 0.0, 320.0, 49.0)]autorelease]; + if (self.aTabBar == nil) { + self.aTabBar = [[[VSTabBar alloc] initWithFrame:CGRectMake(-320.0, 0.0, 320.0, CONTENT_HEIGHT)]autorelease]; + } [self addSubview:self.aTabBar]; - if (self.bTabBar == nil) - self.bTabBar = [[[UITabBar alloc] initWithFrame:CGRectMake(count * 320.0, 0.0, 320.0, 49.0)] autorelease]; + if (self.bTabBar == nil) { + self.bTabBar = [[[VSTabBar alloc] initWithFrame:CGRectMake(count * 320.0, 0.0, 320.0, CONTENT_HEIGHT)] autorelease]; + } [self addSubview:self.bTabBar]; } @@ -77,17 +106,19 @@ - (void)setBounces:(BOOL)bounces { } - (void)setItems:(NSArray *)items animated:(BOOL)animated { - for (UITabBar *tabBar in self.tabBars) { + for (VSTabBar *tabBar in self.tabBars) { int len = 0; - for (int i = [self.tabBars indexOfObject:tabBar] * 5; i < [self.tabBars indexOfObject:tabBar] * 5 + 5; i ++) - if (i < items.count) + for (int i = [self.tabBars indexOfObject:tabBar] * ITEMS_PER_PAGE; i < [self.tabBars indexOfObject:tabBar] * ITEMS_PER_PAGE + ITEMS_PER_PAGE; i ++) { + if (i < items.count) { len ++; + } + } - [tabBar setItems:[items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([self.tabBars indexOfObject:tabBar] * 5, len)]] animated:animated]; + [tabBar setItems:[items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([self.tabBars indexOfObject:tabBar] * ITEMS_PER_PAGE, len)]]]; } - self.contentSize = CGSizeMake(ceil(items.count / 5.0) * 320.0, 49.0); + self.contentSize = CGSizeMake(ceil(items.count / ITEMS_PER_PAGE) * 320.0, CONTENT_HEIGHT); } - (int)currentTabBarTag { @@ -95,7 +126,7 @@ - (int)currentTabBarTag { } - (int)selectedItemTag { - for (UITabBar *tabBar in self.tabBars) + for (VSTabBar *tabBar in self.tabBars) if (tabBar.selectedItem != nil) return tabBar.selectedItem.tag; @@ -104,9 +135,9 @@ - (int)selectedItemTag { } - (BOOL)scrollToTabBarWithTag:(int)tag animated:(BOOL)animated { - for (UITabBar *tabBar in self.tabBars) + for (VSTabBar *tabBar in self.tabBars) if ([self.tabBars indexOfObject:tabBar] == tag) { - UITabBar *tabBar = [self.tabBars objectAtIndex:tag]; + VSTabBar *tabBar = [self.tabBars objectAtIndex:tag]; [self scrollRectToVisible:tabBar.frame animated:animated]; @@ -119,9 +150,10 @@ - (BOOL)scrollToTabBarWithTag:(int)tag animated:(BOOL)animated { return NO; } +/* - (BOOL)selectItemWithTag:(int)tag { - for (UITabBar *tabBar in self.tabBars) - for (UITabBarItem *item in tabBar.items) + for (VSTabBar *tabBar in self.tabBars) + for (VSTabBarItem *item in tabBar.items) if (item.tag == tag) { tabBar.selectedItem = item; @@ -132,6 +164,7 @@ - (BOOL)selectItemWithTag:(int)tag { return NO; } + */ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [infiniTabBarDelegate infiniTabBar:self didScrollToTabBarWithTag:scrollView.contentOffset.x / 320.0]; @@ -141,15 +174,24 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { [self scrollViewDidEndDecelerating:scrollView]; } + +/* - (void)tabBar:(UITabBar *)cTabBar didSelectItem:(UITabBarItem *)item { // Act like a single tab bar - for (UITabBar *tabBar in self.tabBars) + + + for (VSTabBar *tabBar in self.tabBars) if (tabBar != cTabBar) tabBar.selectedItem = nil; [infiniTabBarDelegate infiniTabBar:self didSelectItemWithTag:item.tag]; + [self setNeedsDisplay]; + + } - +*/ + + - (void)dealloc { [bTabBar release]; [aTabBar release]; @@ -158,4 +200,12 @@ - (void)dealloc { [super dealloc]; } + + +#pragma mark VSTabBar Delegate Methods +-(void) tabBar:(VSTabBar*)tabBar selectedItemWithIndex:(NSInteger)index +{ + +} + @end \ No newline at end of file