iOS开发笔录iOS学习ios实用开发技巧

通过UICollectionView来实现无限轮播

2017-06-06  本文已影响129人  41c48b8df394

基本上每个APP都会有无限轮播广告,在这里我通过UICollectionView来实现无线轮播
因为通过UICollectionView实现轮播,一方面是因为UICollectionViewCell的复用,能更好的优化内存。
创建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];
    
}

实现无线轮播轮播方式

#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是为了更好往后滑动,
再说没有用户能需要滑到1W下左右的。。

为了实现外部点击获取当前轮播图的位置 这里添加了个代理,用来外部实现点击效果

@protocol ShufflingDelegate <NSObject>

@optional
//点击某个轮播图的指定的位置
- (void)didSelectWithIndex:(NSInteger)index;

@end

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

上一篇下一篇

猜你喜欢

热点阅读