UIcollectionView 实现 轮番图

#import 

@interface YJCycleView : UIView

@property (nonatomic, strong) NSArray *cycleArr;
@end
#import "YJCycleView.h"
#import "UIView+Extension.h"
#import "YJCycleCollectionViewCell.h"

static NSString *const cycleCell = @"cycleCell";
@interface YJCycleView ()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UICollectionView *collectionView;

@end
@implementation YJCycleView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        [self initView];
    }
    return self;
}

- (void)setCycleArr:(NSArray *)cycleArr {

    _cycleArr = cycleArr;
    
    [self.collectionView reloadData];
    self.pageControl.numberOfPages = cycleArr.count;
    //*10向左滑多少个
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cycleArr.count * 10 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    [self removeCycleTimer];
    [self addCycleTimer];

}

- (void)initView {
    self.autoresizingMask = UIViewAutoresizingNone;
    [self addSubview:self.collectionView];
    [self addSubview:self.pageControl];
}

- (UICollectionView *)collectionView {

    if (_collectionView == nil) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(self.width, self.height);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        [_collectionView registerNib:[UINib nibWithNibName:@"YJCycleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cycleCell];
    }
    
    return _collectionView;
}

- (UIPageControl *)pageControl {

    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.width/2)-(self.cycleArr.count*10)/2, self.height-60, 10, 10)];
        _pageControl.currentPage = 0;
        [_pageControl setPageIndicatorTintColor:[UIColor whiteColor]];
        [_pageControl addTarget:self action:@selector(pageMove:) forControlEvents:UIControlEventValueChanged];
    }
    return _pageControl;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    //*1000设置最大值
    return self.cycleArr.count*1000;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    YJCycleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cycleCell forIndexPath:indexPath];
    
    if (self.cycleArr.count !=0) {
        cell.imageView.image = [UIImage imageNamed:self.cycleArr[indexPath.item % self.cycleArr.count]];
    }
    cell.backgroundColor = indexPath.item % 2 == 0 ? [UIColor redColor] : [UIColor yellowColor];
    return cell;
}

- (void)pageMove:(UIPageControl *)page {
    CGFloat currentOffSetX = self.collectionView.contentOffset.x;
    CGFloat offSetX = currentOffSetX + self.collectionView.width * self.pageControl.currentPage;
    
    [self.collectionView setContentOffset:CGPointMake(offSetX, 0) animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    [self removeCycleTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    [self addCycleTimer];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat ofSetX = scrollView.contentOffset.x + scrollView.bounds.size.width * 0.5;
    self.pageControl.currentPage = (int)(ofSetX / scrollView.bounds.size.width) % (self.cycleArr.count);
}

- (void)removeCycleTimer
{
    // 移除定时器
    [self.timer invalidate];
    self.timer = nil;
}

- (void)addCycleTimer {

    self.timer = [NSTimer timerWithTimeInterval:3.0f target:self selector:@selector(scrollToNext) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)scrollToNext {
    CGFloat currentOffSetX = self.collectionView.contentOffset.x;
    CGFloat offSetX = currentOffSetX + self.collectionView.width;
    
    [self.collectionView setContentOffset:CGPointMake(offSetX, 0) animated:YES];
}

你可能感兴趣的:(UIcollectionView 实现 轮番图)