指示控件之UIPageControl

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

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

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

@class LoopPageView;

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

@interface LoopPageView : UIView
@property(nonatomic, strong) UIPageControl *pageControl;
@property(nonatomic, weak) iddelegate;

- (void)setImages:(NSArray *)images titles:(NSArray *)titles;
@end
@interface LoopPageView ()
{
    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];

你可能感兴趣的:(指示控件之UIPageControl)