Powered By Blogger

Thursday, May 23, 2013

What should be the alternative of horizontal scrolling table in iOS?

Hi,
Dear readers,

Now a days, I came across a new requirement of horizontal scrolling table view. I made some research for it and I found a new library as a unique solution for this. The library is known as "iCarousel" library. You just need to add that 2 files into your project source and after then you can import ".h"(header) file in your class where-ever you want to make such view effect.

Download Link : https://github.com/nicklockwood/iCarousel



After downloading two source files "iCarousel.h" & "iCarousel.m". Follow the below steps to integrate it in your project.

*** Step 1: Create a view of iCarousel in header file and bind it with .XIB file.
    - For ex. : IBOutlet iCarousel *carousel;

*** Step 2: Then place its data-source and delegate methods in implementation section of a view controller.

*** Step 3: Set the delegate of a iCarousel.

*** Step 4: Set the type of a iCarousel in ViewDidLoad method.

- (void)viewDidLoad
{
       [super viewDidLoad];

        carousel.type = iCarouselTypeLinear;
        carousel.vertical = NO;
        carousel.scrollOffset = 1.0;
        carousel.centerItemWhenSelected = NO;
        carousel.stopAtItemBoundary = YES;
        carousel.scrollToItemBoundary = NO;
}

***Step 5: And finally put the below code into implementation section of a view controller.

#pragma mark -------------------
#pragma mark iCarousel Delegate methods
#pragma mark -------------------


- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //generate 100 buttons
    //normally we'd use a backing array
    //as shown in the basic iOS example
    //but for this example we haven't bothered
    return [businessArr count];
}

- (UIView *)carousel:(iCarousel *)carousel1 viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
        //create new view if no view is available for recycling
        CustomBusinessListingCell_iPad *businessListingView  = (CustomBusinessListingCell_iPad *)[[[NSBundle mainBundle]loadNibNamed:@"CustomBusinessListingCell_iPad" owner:self options:nil] objectAtIndex:0];
        businessListingView.frame =  CGRectMake(0, 0, LIST_VIEW_WIDTH, LIST_VIEW_HEIGHT);
        businessListingView.tag = index;
        businessListingView.lblBusinessName.verticalAlignment = BAVerticalAlignmentBottom;

        businessListingView.btnBook.tag = index;
        [businessListingView.btnBook addTarget:self action:@selector(btnBookClicked:) forControlEvents:UIControlEventTouchUpInside];
       
        businessListingView.btnBookmark.tag = index;
        [businessListingView.btnBookmark addTarget:self action:@selector(priceClicked:) forControlEvents:UIControlEventTouchUpInside];
       
         //NSLog(@"BusinessImage : %@",[[businessArr objectAtIndex:index] valueForKey:@"BusinessImage"]);
       
        [businessListingView setBusinessValue:[businessArr objectAtIndex:index]];
       
        if (index==[businessArr count]-1)
        {
            // ask next page only if we haven't reached last page
            if([businessArr count] < totalRecords)
            {
                if (!isLoadmore)
                {
                    isLoadmore = YES;
                    pageIndex = pageIndex+1;
                    [self loadNewData];
                }
            }
        }
        return businessListingView;
}

-(void)setCarouselPosition
{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
    if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
        [carousel scrollToOffset:0.6 duration:0.0];
    }
    else if((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
    {
        [carousel scrollToOffset:1.1 duration:0.0];
    }
}

- (CGFloat)carousel:(iCarousel *)_carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.08f;
        }
        case iCarouselOptionVisibleItems:
        {
            return 5;
        }
        default:
        {
            return value;
        }
    }
}

- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel1
{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
    if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
        if (carousel1.scrollOffset < 0.6) {
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.7];
            [carousel1 scrollToOffset:0.6 duration:0.0];
            [UIView commitAnimations];
        }
    }
    else if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
    {
        if (carousel1.scrollOffset < 1.1) {
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.7];
            [carousel1 scrollToOffset:1.1 duration:0.0];
            [UIView commitAnimations];
        }
    }
}

- (void)carouselDidEndDragging:(iCarousel *)carousel1 willDecelerate:(BOOL)decelerate
{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
    if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
        if (carousel1.scrollOffset < 0.6) {
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.7];
            [carousel1 scrollToOffset:0.6 duration:0.0];
            [UIView commitAnimations];
        }
    }
    else if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
    {
        if (carousel1.scrollOffset < 1.1) {
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.7];
            [carousel1 scrollToOffset:1.1 duration:0.0];
            [UIView commitAnimations];
        }
    }
}

Thanks,
Nilesh M. Prajapati

No comments:

Post a Comment