1
votes

As my title indicated, I want to know if there's a iCarousel delegate method as well as property that I can use to set display time for each item for my banner view when auto-scroll.

Previously, I used SDCycleScrollView for my banner view and I did it this way below:

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {

    BannerData *data = self.bannerDatas[index];
    NSLog(@"***************cycleScrollView************************");
    NSLog(@"index --> %ld", (long)index);
    NSLog(@"banner data --> %@", data);
    NSLog(@"banner data duration --> %d", data.duration);
    cycleScrollView.autoScrollTimeInterval = data.duration;

}

data.duration is duration for each item to stay still. How can I achieve this with iCarousel? Thanks in advance.

Here below are my iCarousel methods so far:

- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel
{
    NSLog(@">>>>>>>>>number of items in Carousel --> %lu", (unsigned long)[self.bannerDatas count]);
    return (NSInteger)[self.bannerDatas count];
}

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{

    view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 520, 375)];

    NSLog(@"url --> %@", [NSURL URLWithString:[self.netImages objectAtIndex:index]]);

    [((UIImageView *)view)sd_setImageWithURL:[NSURL URLWithString:[self.netImages objectAtIndex:index]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    view.contentMode = UIViewContentModeCenter;

    return view;
}

- (void)scrollToItemAtIndex:(NSInteger)index duration:(NSTimeInterval)duration
{
    BannerData *data = self.bannerDatas[index];
    NSLog(@"***************cycleScrollView************************");
    NSLog(@"index --> %ld", (long)index);
    NSLog(@"banner data --> %@", data);
    NSLog(@"banner data duration --> %d", data.duration);
}

- (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 YES;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            //return value * 1.05;
        }
        case iCarouselOptionFadeMax:
        {
//            if (self.carousel.type == iCarouselTypeCustom)
//            {
//                //set opacity based on distance from camera
//                return 0.0;
//            }
//            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
{

    BannerData *data = self.bannerDatas[index];

    if (![data.playlistId isEqualToString:@""]) { //Play Playlist

        CategoryDetailData *detailData = [[CategoryDetailData alloc]init];
        detailData.categoryId = data.playlistId;

        RandomSecondVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"RandomSecondVC"];

        vc.detailData = detailData;

        NSLog(@"vc.detailData.categoryId  --> %@", vc.detailData.categoryId );
        vc.hidesBottomBarWhenPushed = YES; //????sanit
        [self.navigationController pushViewController:vc animated:YES];

    } else if (![data.url isEqualToString:@""]) { //Show webview

        WebViewVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewVC"];
        vc.hidesBottomBarWhenPushed = YES;

        vc.navTitle = @"";
        vc.urlString = data.url;
        [self.navigationController pushViewController:vc animated:YES];

    }else if (![data.videoId isEqualToString:@""]) { //Play video

        VideoDetailVC *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"VideoDetailVC"];

        detailView.videoId = data.videoId;
        detailView.hidesBottomBarWhenPushed = YES; //????sanit
        [self.navigationController pushViewController:detailView animated:YES];

    }
}

Here below is how I place my Carousel view:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *tableCell = nil;

    if (indexPath.section == 0) { //for banner

        //++++++++++++++++++++??sanit iCarousel ver.1++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
        NSLog(@"first section!!!!!!!!");
        static NSString *BannerCellIdentifier = @"BannerTableViewCell"; //original

        BannerTableViewCell *bannerCell = [tableView dequeueReusableCellWithIdentifier:BannerCellIdentifier]; //original
        //BannerTableViewCell *bannerCell = [[BannerTableViewCell alloc]init];

        [self setUpNetImages];
        bannerCell.carouselView.type = iCarouselTypeCoverFlow2;
        //bannerCell.carouselView.type = iCarouselTypeLinear;

        //bannerCell.carouselView.autoscroll = 1; //?????sanit set autoscroll

        bannerCell.carouselView.delegate = self;
        bannerCell.carouselView.dataSource =self;

        //??????sanit to fix banner timing issue first banner
        //BannerData *data0 = self.bannerDatas[1]; //???santi
        //self.cycleScrollView.autoScrollTimeInterval = data0.duration; // use this to fix timing issue of first slide
        //[bannerCell.carouselView scrollToItemAtIndex:0 duration:data0.duration];

        //bannerCell.carouselView.autoscroll = 0.8;
        //[bannerCell.carouselView scrollToItemAtIndex:0 duration:5];
        //[bannerCell.carouselView scrollToItemAtIndex:1 animated:YES];

        [bannerCell.carouselView reloadData];

        tableCell = bannerCell;

        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=//

    } else { //for collection cell below

        NSLog(@"not first section!!!!!!!! %d",(int)indexPath.section);

        static NSString *MainHeaderCellIdentifier = @"MainHeaderTableViewCell";

        MainHeaderTableViewCell *mainHeaderCell = (MainHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MainHeaderCellIdentifier]; //original

        mainHeaderCell.collectionView = (UICollectionView *)[mainHeaderCell viewWithTag:100];
        mainHeaderCell.collectionView.delegate = self;
        mainHeaderCell.collectionView.dataSource = self;

        //NSLog(@"mainHeaderCell.index = %d",(int)mainHeaderCell.index);

        //original
        if (mainHeaderCell.collectionView == nil) {
            NSLog(@"CollectionView Nil!!!!!!!");
        }

        tableCell = mainHeaderCell;
    }

    return tableCell;
}
1

1 Answers

0
votes

Seems that iCarousel does not have that option, but, luckily, you can implement such functionality by yourself:

@property (nonatomic, strong) NSMutableDictionary *operations;
@property (nonatomic) BOOL isDragging;

- (void) carouselDidEndScrollingAnimation:(iCarousel *)carousel
{    
    if (!self.isDragging){
        NSNumber *tag = @(carousel.tag);
        [self.operations[tag] cancel];
        self.operations[tag] = [NSBlockOperation blockOperationWithBlock:^{
            double duration = [self durationForItemAtIndex:carousel.currentItemIndex inCarousel:carousel];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSInteger i = carousel.currentItemIndex + 1;
                NSInteger next = i < carousel.numberOfItems ? i : 0;
                [carousel scrollToItemAtIndex:next animated:YES];
            });
        }];
        [(NSOperation*)(self.operations[tag]) start];
    }
}

- (void) carouselWillBeginDragging:(iCarousel *)carousel
{
    [self.operations[@(carousel.tag)] cancel];
    self.isDragging = YES;
}

- (void) carouselDidEndDragging:(iCarousel *)carousel willDecelerate:   (BOOL)decelerate
{    
    self.isDragging = NO;
}

- (double) durationForItemAtIndex:(NSInteger)index inCarousel:(iCarousel*)carousel
{    
    return <appropriate_duration_for_particular_carousel_and_item_index_in_it>;
}

- (void) startCarousel:(iCarousel*)carousel
{
    [self carouselDidEndScrollingAnimation:carousel];
}

Update -[tableView:cellForRowAtIndexPath:] method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    <...>

    if (indexPath.section == 0) {
        <...>

        bannerCell.carouselView.tag = indexPath.row;
        [self startCarousel:bannerCell.carouselView];
    } else {
        <...>
    }

    return tableCell;
}

And don't forget to initialize the operations dictionary somewhere before usage:

self.operations = [NSMutableDictionary new];

However, all this is kind of draft - you may want to tune and upgrade these snippets according to your needs.