iOS 用UICollectionView来实现自动轮播

  • 使用场景:自定义tableView的headerView,headerView实现了自动轮播

属性和遵守的协议

[@interface](https://my.oschina.net/u/996807) CustomHeaderView ()

[@property](https://my.oschina.net/property) (nonatomic, weak) UICollectionView *collectionView;

[@property](https://my.oschina.net/property) (nonatomic, strong) UIPageControl *pageControl;

[@property](https://my.oschina.net/property) (nonatomic, strong) NSTimer *timer;

[@end](https://my.oschina.net/u/567204)

cell的标识

static NSString *const CustomHeaderViewCellId = @"CustomHeaderViewCellId";

控制器的初始化

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 设置UI
        [self setupUI];
        // 开启定时器
        [self addLCTimer];
    }
    return self;
}

设置UI(高度180,五个item)

- (void)setupUI{
    self.backgroundColor = [UIColor whiteColor];
    
    // 创建collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 180);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 180) collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.pagingEnabled = YES;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.bounces = NO;
    collectionView.contentSize = CGSizeMake(5 * collectionView.bounds.size.width, 0);
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    
    // 注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"CustomHeaderViewCell" bundle:nil] forCellWithReuseIdentifier:CustomHeaderViewCellId];
    
    // pageControl
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    pageControl.numberOfPages = 5;
    CGSize size = [pageControl sizeForNumberOfPages:5];
    pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
    pageControl.center = CGPointMake(self.center.x, 130);
    [self addSubview:pageControl];
    self.pageControl = pageControl;
    
    self.pageControl.currentPage = 0;
}

数据源方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CustomHeaderViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CustomHeaderViewCellId forIndexPath:indexPath];
    cell.model = self.modelsArray[indexPath.row];
    return cell;
}

代理方法

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeLCTimer];
}

/** 当用户停止的时候调用 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addLCTimer];
}

/** 设置页码 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5) % 5;
    self.pageControl.currentPage = page;
}

添加定时器和删除定时器

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

- (void)removeLCTimer{
    [self.timer invalidate];
    self.timer = nil;
}

nextPage(主要的算法)

- (void)nextPage {
    NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
    
    NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:0];
    [self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    NSInteger nextItem = currentIndexPathReset.item + 1;
    if (nextItem == 5) {
        nextItem = 0;
    }
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:0];
    
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

设置数据源

- (void)setModelsArray:(NSArray *)modelsArray{
    _modelsArray = modelsArray;
}

完成以上方法就能实现一个用UICollectionView做的轮播系统,点击跳转的时间自己实现就可以了

你可能感兴趣的:(iOS 用UICollectionView来实现自动轮播)