iCarousel Master with Many Examples

iCarouselExampleViewController.h

#import <UIKit/UIKit.h>

#import “iCarousel.h”

@interface iCarouselExampleViewController : UIViewController <iCarouselDataSource, iCarouselDelegate>

@property (nonatomic, strong) IBOutlet iCarousel *carousel;

@property (nonatomic, strong) IBOutlet UINavigationItem *navItem;

@property (nonatomic, strong) IBOutlet UIBarItem *orientationBarItem;

@property (nonatomic, strong) IBOutlet UIBarItem *wrapBarItem;

– (IBAction)switchCarouselType;

– (IBAction)toggleOrientation;

– (IBAction)toggleWrap;

– (IBAction)insertItem;

– (IBAction)removeItem;

@end

iCarouselExampleViewController.m

#import “iCarouselExampleViewController.h”

#pragma GCC diagnostic ignored “-Wgnu”

@interface iCarouselExampleViewController () <UIActionSheetDelegate>

@property (nonatomic, assign) BOOL wrap;

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation iCarouselExampleViewController

@synthesize carousel;

@synthesize navItem;

@synthesize orientationBarItem;

@synthesize wrapBarItem;

@synthesize wrap;

@synthesize items;

– (void)setUp

{

    //set up data

    self.wrap = YES;

    self.items = [NSMutableArray array];

    for (int i = 0; i < 10; i++)

    {

        [self.items addObject:@(i)];

    }

}

– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))

    {

        [self setUp];

    }

    return self;

}

– (id)initWithCoder:(NSCoder *)aDecoder

{

    if ((self = [super initWithCoder:aDecoder]))

    {

        [self setUp];

    }

    return self;

}

– (void)dealloc

{

    //it’s a good idea to set these to nil here to avoid

    //sending messages to a deallocated viewcontroller

    carousel.delegate = nil;

    carousel.dataSource = nil;

    

}

#pragma mark –

#pragma mark View lifecycle

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    //configure carousel

    self.carousel.type = iCarouselTypeCoverFlow2;

    self.navItem.title = @”CoverFlow2″;

}

– (void)viewDidUnload

{

    [super viewDidUnload];

    self.carousel = nil;

    self.navItem = nil;

    self.orientationBarItem = nil;

    self.wrapBarItem = nil;

}

– (BOOL)shouldAutorotateToInterfaceOrientation:(__unused UIInterfaceOrientation)interfaceOrientation

{

    return YES;

}

– (IBAction)switchCarouselType

{

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@”Select Carousel Type”

                                                       delegate:self

                                              cancelButtonTitle:nil

                                         destructiveButtonTitle:nil

                                              otherButtonTitles:@”Linear”, @”Rotary”, @”Inverted Rotary”, @”Cylinder”, @”Inverted Cylinder”, @”Wheel”, @”Inverted Wheel”, @”CoverFlow”, @”CoverFlow2″, @”Time Machine”, @”Inverted Time Machine”, @”Custom”, nil];

    [sheet showInView:self.view];

}

– (IBAction)toggleOrientation

{

    //carousel orientation can be animated

    [UIView beginAnimations:nil context:nil];

    self.carousel.vertical = !self.carousel.vertical;

    [UIView commitAnimations];

    

    //update button

    self.orientationBarItem.title = self.carousel.vertical? @”Vertical”: @”Horizontal”;

}

– (IBAction)toggleWrap

{

    self.wrap = !self.wrap;

    self.wrapBarItem.title = self.wrap? @”Wrap: ON”: @”Wrap: OFF”;

    [self.carousel reloadData];

}

– (IBAction)insertItem

{

    NSInteger index = MAX(0, self.carousel.currentItemIndex);

    [self.items insertObject:@(self.carousel.numberOfItems) atIndex:(NSUInteger)index];

    [self.carousel insertItemAtIndex:index animated:YES];

}

– (IBAction)removeItem

{

    if (self.carousel.numberOfItems > 0)

    {

        NSInteger index = self.carousel.currentItemIndex;

        [self.items removeObjectAtIndex:(NSUInteger)index];

        [self.carousel removeItemAtIndex:index animated:YES];

    }

}

#pragma mark –

#pragma mark UIActionSheet methods

– (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

{

    if (buttonIndex >= 0)

    {

        //map button index to carousel type

        iCarouselType type = buttonIndex;

        

        //carousel can smoothly animate between types

        [UIView beginAnimations:nil context:nil];

        self.carousel.type = type;

        [UIView commitAnimations];

        

        //update title

        self.navItem.title = [actionSheet buttonTitleAtIndex:buttonIndex];

    }

}

#pragma mark –

#pragma mark iCarousel methods

– (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel

{

    return (NSInteger)[self.items count];

}

– (UIView *)carousel:(__unused 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];

        label.tag = 1;

        [view addSubview:label];

    }

    else

    {

        //get a reference to the label in the recycled view

        label = (UILabel *)[view viewWithTag:1];

    }

    

    //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

    label.text = [self.items[(NSUInteger)index] stringValue];

    

    return view;

}

– (NSInteger)numberOfPlaceholdersInCarousel:(__unused iCarousel *)carousel

{

    //note: placeholder views are only displayed on some carousels if wrapping is disabled

    return 2;

}

– (UIView *)carousel:(__unused iCarousel *)carousel placeholderViewAtIndex:(NSInteger)index reusingView:(UIView *)view

{

    UILabel *label = nil;

    

    //create new view if no view is available for recycling

    if (view == nil)

    {

        //don’t do anything specific to the index within

        //this `if (view == nil) {…}` statement because the view will be

        //recycled and used with other index values later

        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.0f];

        label.tag = 1;

        [view addSubview:label];

    }

    else

    {

        //get a reference to the label in the recycled view

        label = (UILabel *)[view viewWithTag:1];

    }

    

    //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

    label.text = (index == 0)? @”[“: @”]”;

    

    return view;

}

– (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform

{

    //implement ‘flip3D’ style carousel

    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);

    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.carousel.itemWidth);

}

– (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value

{

    //customize carousel display

    switch (option)

    {

        case iCarouselOptionWrap:

        {

            //normally you would hard-code this to YES or NO

            return self.wrap;

        }

        case iCarouselOptionSpacing:

        {

            //add a bit of spacing between the item views

            return value * 1.05f;

        }

        case iCarouselOptionFadeMax:

        {

            if (self.carousel.type == iCarouselTypeCustom)

            {

                //set opacity based on distance from camera

                return 0.0f;

            }

            return value;

        }

        case iCarouselOptionShowBackfaces:

        case iCarouselOptionRadius:

        case iCarouselOptionAngle:

        case iCarouselOptionArc:

        case iCarouselOptionTilt:

        case iCarouselOptionCount:

        case iCarouselOptionFadeMin:

        case iCarouselOptionFadeMinAlpha:

        case iCarouselOptionFadeRange:

        case iCarouselOptionOffsetMultiplier:

        case iCarouselOptionVisibleItems:

        {

            return value;

        }

    }

}

#pragma mark –

#pragma mark iCarousel taps

– (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index

{

    NSNumber *item = (self.items)[(NSUInteger)index];

    NSLog(@”Tapped view number: %@”, item);

}

– (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel

{

    NSLog(@”Index: %@”, @(self.carousel.currentItemIndex));

}

@end

iCarouselExampleAppDelegate.h

#import <UIKit/UIKit.h>

@class iCarouselExampleViewController;

@interface iCarouselExampleAppDelegate : NSObject <UIApplicationDelegate>

@property (nonatomic, strong) IBOutlet UIWindow *window;

@property (nonatomic, strong) IBOutlet iCarouselExampleViewController *viewController;

@end

iCarouselExampleAppDelegate.m

#import “iCarouselExampleAppDelegate.h”

#import “iCarouselExampleViewController.h”

@implementation iCarouselExampleAppDelegate

@synthesize window;

@synthesize viewController;

– (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWithOptions:(__unused NSDictionary *)launchOptions

{

    [self.window addSubview:self.viewController.view];

    [self.window makeKeyAndVisible];

    return YES;

}

@end

Download Sample Project From Github

Leave a comment