scrollpaginationuicollectionview

UICollectionView Auto Scroll Paging


my project have a UIcollectionView. This Horizontal paging and have four object. I want my collectionView Auto Scroll Paging. which use methods?


Solution

  • As for theory, I just explain the key point. For example, if you want to auto-cycle display 5 images in somewhere, just as ads bar. You can create a UICollectionView with 100 sections, and there're 5 items in every section. After creating UICollectionView, set its original position is equal to the 50th section 0th item(middle of MaxSections ) so that you can scroll to left or right. Don't worry it will ends, because I have reset the position equal to middle of MaxSections when the Timer runs. Never argue with me :" it also will ends when the user keep scrolling by himself" If one find there're only 5 images, will he keep scrolling!? I believe there is few such silly user in this world!

    Note: In the following codes,the headerView is UICollectionView,headerNews is data. And I suggest set MaxSections = 100.

    Add a NSTimer after custom collectionView(Ensure the UICollectionViewFlowLayout is properly set at first):

        - (void)addTimer
        {
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
            [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
            self.timer = timer;
    
    }
    

    then implement @selector(nextPage):

     - (void)nextPage
    {
        // 1.back to the middle of sections
        NSIndexPath *currentIndexPathReset = [self resetIndexPath];
    
        // 2.next position 
        NSInteger nextItem = currentIndexPathReset.item + 1;
        NSInteger nextSection = currentIndexPathReset.section;
        if (nextItem == self.headerNews.count) {
            nextItem = 0;
            nextSection++;
        }
        NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    
        // 3.scroll to next position
        [self.headerView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
    

    last implement resetIndexPath method:

    - (NSIndexPath *)resetIndexPath
    {
        // currentIndexPath
        NSIndexPath *currentIndexPath = [[self.headerView indexPathsForVisibleItems] lastObject];
        // back to the middle of sections
        NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:MaxSections / 2];
        [self.headerView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        return currentIndexPathReset;
    }
    

    In order to control NSTimer , you have to implement some other methods and UIScrollView's delegate methods:

    - (void)removeTimer
    {
        // stop NSTimer 
        [self.timer invalidate];
       // clear NSTimer
        self.timer = nil;
    }
    
    // UIScrollView' delegate method
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
            [self removeTimer];
    }
    
    
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
            [self addTimer];
    
    }