Today we will learn how to create iCarousel in iOS.

Steps:

  1.  Create a new Xcode Project Lets say (CarouselDemo).
  2.  Now go to this Github link and download the iCarousel Project. Unzip it and add two files iCarousel.h and iCarousel.m in your project.
  3.  Now go to StoryBoard and  take a UIView and two labels.

Screen Shot 2016-09-21 at 12.08.03 AM.png4. Now change the class of UIView to iCarousel.

Screen Shot 2016-09-21 at 12.08.26 AM.png5. Make Proper connection of the StoryBoard.

Screen Shot 2016-09-21 at 12.09.27 AM.png6.  One thing here you have to note down that, Don’t connect dataSource and delegate from StoryBoard, otherWise it will call the delegate method of iCarousel even before calling ViewDidLoad. So we will set it manually from ViewDidLoad.

Now go to Your ViewController.m and  write the following method

#import “ViewController.h”

#import “iCarousel.h”

@interface ViewController ()<iCarouselDataSource, iCarouselDelegate>

@property (weak, nonatomic) IBOutlet iCarousel *iCarouselView;

@property (weak, nonatomic) IBOutlet UILabel *selectedCarouselLabel;

@property (nonatomic, strong) NSMutableArray *iCarouselItems;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    //configure carousel

    _iCarouselItems = [[NSMutableArray alloc]init];

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

    {

        [_iCarouselItems addObject:@(i)];

    }

    self.iCarouselView.dataSource = self;

    self.iCarouselView.delegate = self;

    _iCarouselView.type = iCarouselTypeCoverFlow2;

   // self.items = [NSMutableArray array];

    

    // Do any additional setup after loading the view, typically from a nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return YES;

}

#pragma mark –

#pragma mark iCarousel methods

– (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel

{

    //return the total number of items in the carousel

    return [_iCarouselItems 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)

    {

        //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 = NSTextAlignmentCenter;

        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 = [_iCarouselItems[index] stringValue];

    

    return view;

}

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

{

    if (option == iCarouselOptionSpacing)

    {

        return value * 1.1;

    }

    return value;

}

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

    NSLog(@”Selected carousel index is %ld”,(long)index);

    self.selectedCarouselLabel.text = [NSString stringWithFormat:@”Selected Index is %ld”,(long)index];

}

@end

7. Now run the code and see the desired output.

Simulator Screen Shot Sep 21, 2016, 1.39.42 AM.png

 Please do share your reviews and comment.

You can download the whole source code from here.

Enjoy Coding.  🙂

Advertisements