UICollectionView 用作轮番图的实现,demo 地址:https://github.com/SummerHH/YJCYCleCollectionVIew
#import <UIKit/UIKit.h> @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 ()<UICollectionViewDelegate,UICollectionViewDataSource>
@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 * inSection:];
[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 = ;
layout.minimumInteritemSpacing = ;
layout.itemSize = CGSizeMake(self.width, self.height);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(, , 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/)-(self.cycleArr.count*)/, self.height-, , )];
_pageControl.currentPage = ;
[_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*;
} - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YJCycleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cycleCell forIndexPath:indexPath]; if (self.cycleArr.count !=) {
cell.imageView.image = [UIImage imageNamed:self.cycleArr[indexPath.item % self.cycleArr.count]];
}
cell.backgroundColor = indexPath.item % == ? [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, ) 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, ) animated:YES];
}