Skip to content

Instantly share code, notes, and snippets.

@serhatsezer
Created June 30, 2015 09:06
Show Gist options
  • Save serhatsezer/0ad0f1364af4b08ec07e to your computer and use it in GitHub Desktop.
Save serhatsezer/0ad0f1364af4b08ec07e to your computer and use it in GitHub Desktop.
icarousel implementation file
- (void)configureContainers
{
_containerRect = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.view.bounds), self.tabBarHeight);
// Tabbar container view
_tabButtonsContainerView = [[UIView alloc] initWithFrame:_containerRect];
_tabButtonsContainerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_tabButtonsContainerView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.8f];
[self.view addSubview:_tabButtonsContainerView];
// Adding carousel manually
_headerCarousel = [[iCarousel alloc] initWithFrame:_containerRect];
_headerCarousel.type = iCarouselTypeLinear;
_headerCarousel.scrollSpeed = 0.1f;
_headerCarousel.decelerationRate = 0.5f;
_headerCarousel.centerItemWhenSelected = YES;
_headerCarousel.stopAtItemBoundary = YES;
_headerCarousel.delegate = self;
_headerCarousel.dataSource = self;
_headerCarousel.currentItemIndex = CURRENT_SELECTED_CATEGORY; // setting current index for header carousel view
[_tabButtonsContainerView addSubview:_headerCarousel];
// Right indicator
_rightIndicator = [UIButton buttonWithType:UIButtonTypeCustom];
_rightIndicator.frame = CGRectMake(CGRectGetMaxX(_tabButtonsContainerView.frame) - 60.0f, 0.0f, 60.0f, 30.0f);
_rightIndicator.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"segmented_right_arrow"]];
[_rightIndicator addTarget:self action:@selector(goSegmentedForward:) forControlEvents:UIControlEventTouchUpInside];
[_tabButtonsContainerView addSubview:_rightIndicator];
// Left indicator
_leftIndicator = [UIButton buttonWithType:UIButtonTypeCustom];
_leftIndicator.frame = CGRectMake(0.0f, 0.0f, 60.0f, 30.0f);
_leftIndicator.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"segmented_left_arrow"]];
[_leftIndicator addTarget:self action:@selector(goSegmentedBackward:) forControlEvents:UIControlEventTouchUpInside];
[_tabButtonsContainerView addSubview:_leftIndicator];
/**
The content fills the whole screen so the buttons can be animated in and out.
Scrollviews set as the content view should use contentInset property to avoid being overlapped by the buttons.
*/
_containerRect.origin.y = self.tabBarHeight / 2;
_containerRect.size.height = self.view.bounds.size.height;
_contentContainerView = [[UIView alloc] initWithFrame:_containerRect];
_contentContainerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_contentContainerView.backgroundColor = [UIColor clearColor];
// Main content carousel
_contentCarousel = [[iCarousel alloc] initWithFrame:_containerRect];
_contentCarousel.delegate = self;
_contentCarousel.dataSource = self;
_contentCarousel.currentItemIndex = CURRENT_SELECTED_CATEGORY;
_headerCarousel.scrollSpeed = 0.5f;
_headerCarousel.decelerationRate = 1.0;
_headerCarousel.scrollToItemBoundary = YES;
_headerCarousel.centerItemWhenSelected = YES;
_headerCarousel.scrollEnabled = YES;
_headerCarousel.type = iCarouselTypeLinear;
[self.view addSubview:_contentContainerView];
[_contentContainerView addSubview:_contentCarousel];
[self.view bringSubviewToFront:_tabButtonsContainerView];
}
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return _categories.count;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
if(carousel == self.contentCarousel) {
self.contentVC = [[ContentVC alloc] init]; // this is basically view controller
[self addChildViewController:self.contentVC];
[self.contentVC didMoveToParentViewController:self];
return self.contentVC.collectionView;
}
if(carousel == self.headerCarousel) {
view = [TRUSegmentViewGenerator segmentedViewWithTitle:_categories[index] withParentView:self.tabButtonsContainerView]; // tabButtonsContainerView is a holder view.
return view;
}
return view;
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value {
if(carousel == self.headerCarousel) {
switch (option) {
case iCarouselOptionWrap : return YES;
case iCarouselOptionSpacing : return value * 1.05f;
default: break;
}
}
else if(carousel == self.contentCarousel) {
switch (option) {
case iCarouselOptionWrap: return YES;
case iCarouselOptionSpacing: return value * 1.0f;
default: break;
}
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment