iOS轮播图的封装

2017-08-22  本文已影响88人  Maj_sunshine

github下载地址https://github.com/maajian/AJCarousel

主要功能 : 图片轮播
封装其他效果 :
1 设置分页控制器的颜色
2 设置分页控制器当前页面选中的颜色
3 是否隐藏分页控制器
4 图片切换时间设置
5 是否自动轮播

主要代码

初始化创建

- (instancetype)initWithFrame:(CGRect)frame
         collectionViewLayout:  (UICollectionViewLayout *)layout
                   imageArray:(NSArray *)imageArray
                        View : (UIView *)View{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        /*! 设置代理 */
        self.dataSource =self;
        self.delegate =self;
        /*! 默认图片滑动时间为2 */
        _timeInterval = 2.0  ;
        self.imageArray = imageArray;
        /*! 不显示滑动条 */
        self.showsHorizontalScrollIndicator = NO;
        /*! 做分页处理,很重要 */
        self.pagingEnabled = YES;
        self.backgroundColor = [UIColor whiteColor];
        /*! 滑动到指定位置 */
        [self registerClass:[AJCollectionViewCell class] forCellWithReuseIdentifier:cellID];
        [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        [View insertSubview:self.pageControl atIndex:5];
        [self addTimer];
    }
    return self;
}

CollectionView代理方法实现

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AJCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {
        cell = [[AJCollectionViewCell alloc]init];
    }
    cell.currentImage = _imageArray[indexPath.row];
    /*! 实际使用过程中可以把label移除 , 避免cell上存在多余的子视图 */
    cell.currentLabelStr = [NSString stringWithFormat:@"这是第%li张图片",(long)indexPath.item + 1];
    
    return cell;
}

定时器调用方法

- (void)pageChange {
    /*! 计算当前位置 */
    NSIndexPath *indexpath = [self indexPathsForVisibleItems].lastObject;
    NSIndexPath *currentIndexpath = [NSIndexPath indexPathForItem:indexpath.item inSection:1];
    [self scrollToItemAtIndexPath:currentIndexpath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    NSInteger nextItem = currentIndexpath.item + 1 ;
    NSInteger nextSection = 1 ;
    /*! 如果为最后一个图片 */
    if (nextItem==self.imageArray.count) {
        nextItem=0;
        nextSection++;
    }
    [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:nextSection] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

属性的重写

- (void)setPageTintColor:(UIColor *)pageTintColor{
    _pageControl.pageIndicatorTintColor = pageTintColor;
}
- (UIColor *)pageTintColor{
    return _pageControl.pageIndicatorTintColor;
}

- (void)setCurrentPageColor:(UIColor *)currentPageColor{
    _pageControl.currentPageIndicatorTintColor = currentPageColor;
}

- (UIColor *)currentPageColor{
    return _pageControl.currentPageIndicatorTintColor;
}
- (void)setHiddenPageCtr:(BOOL)hiddenPageCtr{
    _pageControl.hidden = hiddenPageCtr;
}

- (void)setDuration:(NSTimeInterval)duration{
    [self removeTimer];
    _timeInterval = duration;
    [self addTimer];
}
- (NSTimeInterval)duration{
    return _timeInterval;
}

效果图

轮播.gif

具体实现查看[github]https://github.com/maajian/AJCarousel

有bug或者功能不对希望提出

上一篇下一篇

猜你喜欢

热点阅读