UICollectionView自动滚动分页 [英] UICollectionView Auto Scroll Paging

查看:659
本文介绍了UICollectionView自动滚动分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有一个UIcollectionView.此水平分页并具有四个对象.我想要我的收藏查看自动滚动分页.哪种使用方法?

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

推荐答案

至于理论,我只是解释关键点.例如,如果您想在某个位置自动循环显示5张图像,就像广告栏一样.您可以创建一个包含100个部分的UICollectionView,每个部分有5个项目.创建UICollectionView后,将其原始位置设置为第50个第0个项目(MaxSections的中间) ),以便您可以向左或向右滚动.不必担心它会结束,因为在计时器运行时,我已将位置重置为与MaxSections的中间位置相等.永远不要跟我争论:当用户继续滚动时,它也将结束".如果发现只有5张图像,他会继续滚动吗?我相信这个世界上很少有如此愚蠢的用户!

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!

注意:在以下代码中,headerView是UICollectionView,headerNews是数据.我建议将MaxSections设置为100.

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

在自定义collectionView之后添加NSTimer(确保首先正确设置UICollectionViewFlowLayout):

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;

}

然后实现@selector(nextPage):

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];
}

最后实现resetIndexPath方法:

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;
}

为了控制NSTimer,您必须实现一些其他方法和UIScrollView的委托方法:

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];

}

这篇关于UICollectionView自动滚动分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆