通過UICollectionView來實(shí)現(xiàn)無限輪播

基本上每個(gè)APP都會(huì)有無限輪播廣告,在這里我通過UICollectionView來實(shí)現(xiàn)無線輪播
因?yàn)橥ㄟ^UICollectionView實(shí)現(xiàn)輪播,一方面是因?yàn)閁ICollectionViewCell的復(fù)用,能更好的優(yōu)化內(nèi)存。
創(chuàng)建UICollectionView

- (void)setupUI{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 5, kScreenWidth-20, kScrollViewH-10) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(kScreenWidth -20, kScrollViewH-10);
    layout.minimumLineSpacing = 0;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    [self addSubview:_collectionView];
    
    self.pageControl = [[UIPageControl alloc]init];
    self.pageControl.frame = CGRectMake(0, kScrollViewH - 30, kScreenWidth, 30);
    [self addSubview:self.pageControl];
   NSTimer *timer  = [NSTimer scheduledTimerWithTim
eInterval:2.0 target:self selector:@selector(timerMethed) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:timer forMode:NSRunLoopCommonModes];
    
}

實(shí)現(xiàn)無線輪播輪播方式

#pragma mark - collection Delegate
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    cell.backgroundColor = numArray[indexPath.row%numArray.count];
    return  cell;
}



- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return numArray.count *10000;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate didSelectWithIndex:indexPath.row%numArray.count];
    
}

#pragma mark - ScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offset_x = scrollView.contentOffset.x/(kScreenWidth - 20);
    
    if (offset_x>numArray.count) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        
    }else if(offset_x <0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:numArray.count  inSection:0];
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offset_x = scrollView.contentOffset.x/(kScreenWidth - 20);
    if (offset_x>numArray.count-1) {
        _pageControl.currentPage = (NSInteger)offset_x%numArray.count;
        
    }else{
        _pageControl.currentPage = offset_x;
        
    }
    
}

我在numberOfItemsInSection*10000是為了更好往后滑動(dòng),
再說沒有用戶能需要滑到1W下左右的。。

為了實(shí)現(xiàn)外部點(diǎn)擊獲取當(dāng)前輪播圖的位置 這里添加了個(gè)代理,用來外部實(shí)現(xiàn)點(diǎn)擊效果

@protocol ShufflingDelegate <NSObject>

@optional
//點(diǎn)擊某個(gè)輪播圖的指定的位置
- (void)didSelectWithIndex:(NSInteger)index;

@end

Demo地址:https://github.com/lanjiaoli/ShullfingScrollView.git
有不足的地方,大家可以提意見。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容