custom tabbars and make them circulate
- by pengwang
i want to custom tabbars and want to circulate slide,default tab bar only have 5 items show at the same time,it not meet me,i have 11 items,so i want to make 3 tabbars ,every have 5 items,for example A(0-4)--B(5-9)--C(10)--A--B--C--A. at print i only finish A(0-4)--B(5-9)--C(10),how to make them circulate? my code : .h file
#import <UIKit/UIKit.h>
@protocol InfiniTabBarDelegate;
@interface InfiniTabBar : UIScrollView <UIScrollViewDelegate, UITabBarDelegate> {
__weak id <InfiniTabBarDelegate> infiniTabBarDelegate;
NSMutableArray *tabBars;
UITabBar *aTabBar;
UITabBar *bTabBar;
}
@property (nonatomic, weak) id infiniTabBarDelegate;
@property (strong,nonatomic) NSMutableArray *tabBars;
@property (strong,nonatomic) UITabBar *aTabBar;
@property (strong,nonatomic) UITabBar *bTabBar;
- (id)initWithItems:(NSArray *)items;
- (void)setBounces:(BOOL)bounces;
// Don't set more items than initially
- (void)setItems:(NSArray *)items animated:(BOOL)animated;
- (int)currentTabBarTag;
- (int)selectedItemTag;
- (BOOL)scrollToTabBarWithTag:(int)tag animated:(BOOL)animated;
- (BOOL)selectItemWithTag:(int)tag;
@end
@protocol InfiniTabBarDelegate <NSObject>
- (void)infiniTabBar:(InfiniTabBar *)tabBar didScrollToTabBarWithTag:(int)tag;
- (void)infiniTabBar:(InfiniTabBar *)tabBar didSelectItemWithTag:(int)tag;
@end
.m file
@implementation InfiniTabBar
@synthesize infiniTabBarDelegate;
@synthesize tabBars;
@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.
if (self) {
self.pagingEnabled = YES;
self.delegate = self;
self.tabBars = [[NSMutableArray alloc] init];
float x = 0.0;
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;
int len = 0;
for (int i = d * 5; i < d * 5 + 5; i ++)
if (i < items.count)
len ++;
tabBar.items = [items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(d * 5, len)]];
// NSLog(@"####%@",NSMakeRange(d * 5, len));
[self.tabBars addObject:tabBar];
[self addSubview:tabBar];
x += 320.0;
}
self.contentSize = CGSizeMake(x, 49.0);
}
return self;
}
- (void)setBounces:(BOOL)bounces {
if (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)];
[self addSubview:self.aTabBar];
if (self.bTabBar == nil)
self.bTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(count * 320.0, 0.0, 320.0, 49.0)];
[self addSubview:self.bTabBar];
}
} else {
[self.aTabBar removeFromSuperview];
[self.bTabBar removeFromSuperview];
}
[super setBounces:bounces];
}
- (void)setItems:(NSArray *)items animated:(BOOL)animated {
for (UITabBar *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)
len ++;
[tabBar setItems:[items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([self.tabBars indexOfObject:tabBar] * 5, len)]] animated:animated];
}
self.contentSize = CGSizeMake(ceil(items.count / 5.0) * 320.0, 49.0);
}
- (int)currentTabBarTag {
return self.contentOffset.x / 320.0;
}
- (int)selectedItemTag {
for (UITabBar *tabBar in self.tabBars)
if (tabBar.selectedItem != nil)
return tabBar.selectedItem.tag;
// No item selected
return 0;
}
- (BOOL)scrollToTabBarWithTag:(int)tag animated:(BOOL)animated {
for (UITabBar *tabBar in self.tabBars)
if ([self.tabBars indexOfObject:tabBar] == tag) {
UITabBar *tabBar = [self.tabBars objectAtIndex:tag];
[self scrollRectToVisible:tabBar.frame animated:animated];
if (animated == NO)
[self scrollViewDidEndDecelerating:self];
return YES;
}
return NO;
}
- (BOOL)selectItemWithTag:(int)tag {
for (UITabBar *tabBar in self.tabBars)
for (UITabBarItem *item in tabBar.items)
if (item.tag == tag) {
tabBar.selectedItem = item;
[self tabBar:tabBar didSelectItem:item];
return YES;
}
return NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[infiniTabBarDelegate infiniTabBar:self didScrollToTabBarWithTag:scrollView.contentOffset.x / 320.0];
}
- (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)
if (tabBar != cTabBar)
tabBar.selectedItem = nil;
[infiniTabBarDelegate infiniTabBar:self didSelectItemWithTag:item.tag];
}
@end