Created
June 22, 2015 22:14
-
-
Save serhatsezer/0d5200b18d61310d62dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// iCarouselExampleViewController.m | |
// iCarouselExample | |
// | |
// Created by Nick Lockwood on 03/04/2011. | |
// Copyright 2011 Charcoal Design. All rights reserved. | |
// | |
#import "iCarouselExampleViewController.h" | |
#import "TableViewController.h" | |
#import "SampleViewController.h" | |
@interface iCarouselExampleViewController () | |
@property (nonatomic, strong) NSMutableArray *items1; | |
@property (nonatomic, strong) NSMutableArray *items2; | |
@property (nonatomic, strong) TableViewController *tableViewSample; | |
@property (nonatomic, strong) SampleViewController *sampleVc; | |
@property (nonatomic, strong) UIView *pageContainerView; | |
@end | |
@implementation iCarouselExampleViewController | |
@synthesize carousel1; | |
@synthesize carousel2; | |
@synthesize items1; | |
@synthesize items2; | |
- (void)awakeFromNib | |
{ | |
_tableViewSample = [[TableViewController alloc] initWithStyle:UITableViewStyleGrouped]; | |
_sampleVc = [[SampleViewController alloc] init]; | |
//set up data sources | |
self.items1 = [NSMutableArray array]; | |
for (int i = 0; i < 20; i++) | |
{ | |
[items1 addObject:[NSNumber numberWithInt:i]]; | |
} | |
self.items2 = [NSMutableArray array]; | |
for (int i = 0; i < 20; i++) | |
{ | |
[items2 addObject:[NSString stringWithFormat:@"cat %d", i]]; | |
} | |
} | |
- (void)dealloc | |
{ | |
//it's a good idea to set these to nil here to avoid | |
//sending messages to a deallocated viewcontroller | |
carousel1.delegate = nil; | |
carousel1.dataSource = nil; | |
carousel2.delegate = nil; | |
carousel2.dataSource = nil; | |
} | |
#pragma mark - | |
#pragma mark View lifecycle | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
//configure carousel | |
carousel1.type = iCarouselTypeLinear; | |
carousel1.currentItemIndex = 5; | |
carousel1.scrollToItemBoundary = YES; | |
carousel1.scrollSpeed = 0.50; | |
carousel1.decelerationRate = 0.90; | |
carousel2 = [[iCarousel alloc] initWithFrame:CGRectMake(0.0f, 320.0f, 320.0f, 210.0f)]; | |
carousel2.delegate = self; | |
carousel2.dataSource = self; | |
carousel2.type = iCarouselTypeLinear; | |
carousel2.decelerationRate = 1.90; | |
carousel2.scrollToItemBoundary = YES; | |
carousel2.centerItemWhenSelected = YES; | |
carousel2.currentItemIndex = 5; | |
_pageContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 320.0f, 320.0f, 210.0f)]; | |
// _pageContainerView.backgroundColor = [UIColor redColor]; | |
_pageContainerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | |
_pageContainerView.clipsToBounds = YES; | |
[self.view addSubview:_pageContainerView]; | |
[self.view addSubview:carousel2]; | |
[_pageContainerView setNeedsDisplay]; | |
} | |
- (void)viewDidUnload | |
{ | |
[super viewDidUnload]; | |
//free up memory by releasing subviews | |
self.carousel1 = nil; | |
self.carousel2 = nil; | |
} | |
#pragma mark - | |
#pragma mark iCarousel methods | |
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel | |
{ | |
//return the total number of items in the carousel | |
if (carousel == carousel1) | |
{ | |
return [items1 count]; | |
} | |
else | |
{ | |
return [items2 count]; | |
} | |
} | |
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view | |
{ | |
UILabel *label = nil; | |
//create new view if no view is available for recycling | |
if (view == nil) | |
{ | |
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)]; | |
((UIImageView *)view).image = [UIImage imageNamed:@"page.png"]; | |
view.contentMode = UIViewContentModeCenter; | |
label = [[UILabel alloc] initWithFrame:view.bounds]; | |
label.backgroundColor = [UIColor clearColor]; | |
label.textAlignment = UITextAlignmentCenter; | |
label.font = [label.font fontWithSize:50]; | |
[_sampleVc viewWillAppear:YES]; | |
[self addChildViewController:_sampleVc]; | |
[view addSubview:_sampleVc.view]; | |
[_sampleVc didMoveToParentViewController:self]; | |
[_sampleVc viewDidAppear:YES]; | |
[view addSubview:label]; | |
} | |
else | |
{ | |
label = [[view subviews] lastObject]; | |
} | |
//set item label | |
//remember to always set any properties of your carousel item | |
//views outside of the `if (view == nil) {...}` check otherwise | |
//you'll get weird issues with carousel item content appearing | |
//in the wrong place in the carousel | |
if (carousel == carousel1) | |
{ | |
//items in this array are numbers | |
label.text = [[items1 objectAtIndex:index] stringValue]; | |
} | |
else | |
{ | |
//items in this array are strings | |
label.text = [items2 objectAtIndex:index]; | |
} | |
return view; | |
} | |
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value | |
{ | |
//customize carousel display | |
switch (option) | |
{ | |
case iCarouselOptionSpacing: | |
{ | |
if ((carousel == carousel2) || (carousel == carousel1)) | |
{ | |
//add a bit of spacing between the item views | |
return value * 1.10f; | |
} | |
} | |
case iCarouselOptionWrap: | |
{ | |
return YES; | |
} | |
default: | |
{ | |
return value; | |
} | |
} | |
} | |
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel { | |
[carousel1 scrollToItemAtIndex:carousel.currentItemIndex animated:YES]; | |
// NSLog(@"carousel current item index did change. %zd",carousel.currentItemIndex); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment