指示控件之UIPageControl

特性:无限轮播的what's new,含有icon和title

方案一:使用UIScrollView,同时创建n个contentView
方案二:使用UICollectionView,创建若干干contentView,然后复用

这里只介绍相对优雅的方案二

@class LoopPageView;

@protocol LoopPageViewDelegate <NSObject>
- (void)loopPageView:(LoopPageView *)pageView didSelectItemAtIndex:(NSInteger)index;
@end

@interface LoopPageView : UIView
@property(nonatomic, strong) UIPageControl *pageControl;
@property(nonatomic, weak) id<LoopPageViewDelegate>delegate;

- (void)setImages:(NSArray *)images titles:(NSArray *)titles;
@end
@interface LoopPageView ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSArray *_images;
    NSArray *_titles;
    NSInteger _totalItemCount;
    UICollectionViewFlowLayout *_flowLayout;
    UICollectionView *_collectionView;
    NSTimer *_timer;
}
@end

static NSString *kCollectionViewCellIdentify = @"kCollectionViewCell";

@implementation LoopPageView
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.sectionInset = UIEdgeInsetsZero;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:_flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate  = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [_collectionView registerClass:[ImageTextCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellIdentify];
        [self addSubview:_collectionView];
        
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.pageIndicatorTintColor = HexColor(@"#D9D9D9");
        _pageControl.currentPageIndicatorTintColor = HexColor(@"#42C642");
        [self addSubview:_pageControl];
    }
    
    return self;
}

- (void)dealloc
{
    [self invalidateTimer];
}

- (void)setImages:(NSArray *)images titles:(NSArray *)titles
{
    if (images.count != titles.count) {
        NSAssert(0, @"imagesCount != titlesCount");
    }
    
    _images = images;
    _titles = titles;
    _totalItemCount = _images.count;
    if (images.count != 1) {
        [self setupTimer];
        
        _pageControl.numberOfPages = images.count;
    }
    
    [_collectionView reloadData];
}

- (void)setupTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)autoScroll
{
    NSInteger targetIndex = [self currentIndex] + 1;
    targetIndex = (targetIndex >= _totalItemCount) ? targetIndex %= _totalItemCount : targetIndex;
    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

- (NSInteger)currentIndex
{
    if (_collectionView.frame.size.width == 0) return 0;
    
    return (_collectionView.contentOffset.x + _flowLayout.itemSize.width * 0.5) / _flowLayout.itemSize.width;
}

- (void)invalidateTimer
{
    [_timer invalidate];
    _timer = nil;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _flowLayout.itemSize = self.frame.size;
    _collectionView.frame = self.bounds;
    
    if (_collectionView.contentOffset.x == 0 && _totalItemCount) {
        NSInteger tarIndex = _totalItemCount * 0.5;
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:tarIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
    
    if (CGRectEqualToRect(_pageControl.frame, CGRectZero)) {
        _pageControl.frame = CGRectMake((self.width - 100)/2, self.height - 30, 100, 20);
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _totalItemCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIImage *image = _images[indexPath.item % _images.count];
    NSString *title = _titles[indexPath.item % _images.count];
    ImageTextCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellIdentify forIndexPath:indexPath];
    [cell updateImage:image title:title];
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = indexPath.item % _images.count;
    if ([_delegate respondsToSelector:@selector(loopPageView:didSelectItemAtIndex:)]) {
        [_delegate loopPageView:self didSelectItemAtIndex:index];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = [self currentIndex];
    _pageControl.currentPage = index % _images.count;
}

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

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self setupTimer];
}
@end

特性:修改UIPageControl小圆点的颜色

_pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
_pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 2月9日上午6点从淄博出发中午1点钟抵达三亚凤凰机场
    春华秋实0319阅读 236评论 0 0
  • 智障日记阅读 256评论 0 3
  • 很久之前,我一直觉得在平行的世界里有另一个我。她长得和我一样,也喜欢逃课去书店待一个下午。她可能会成为画家,或者音...
    喵小馨阅读 420评论 0 1
  • 三分邪,五分正,二分呆萌可爱。 昨日黄昏。心绪一动,长发变短发。夜里一个悔,不能眠。为即将到来的四月,吊带衫,吊带...
    青衫湿旧阅读 307评论 35 19