#import <UIKit/UIKit.h>
@interface XMGInfiniteScrollView : UIView
/** 需要显示的图片数据(要求里面存放UIImage对象) */
@property (nonatomic, strong) NSArray *images;
@end
#import "XMGInfiniteScrollView.h"
#import "XMGImageCell.h"
@interface XMGInfiniteScrollView() <UICollectionViewDataSource, UICollectionViewDelegate>
/** 定时器 */
@property (nonatomic, weak) NSTimer *timer;
/** 用来显示图片的collectionView */
@property (nonatomic, weak) UICollectionView *collectionView;
@end
@implementation XMGInfiniteScrollView
static NSInteger XMGItemCount = 100;
static NSString * const XMGImageCellId = @"XMGImageCell";
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = 0;
// UICollectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
collectionView.dataSource = self;
collectionView.pagingEnabled = YES;
collectionView.delegate = self;
collectionView.showsHorizontalScrollIndicator = NO;
collectionView.showsVerticalScrollIndicator = NO;
[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGImageCell class]) bundle:nil] forCellWithReuseIdentifier:XMGImageCellId];
[self addSubview:collectionView];
self.collectionView = collectionView;
}
return self;
}
- (void)setImages:(NSArray *)images
{
_images = images;
// 设置默认显示最中间的图片
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:(XMGItemCount * self.images.count) / 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
// 开启定时器
[self startTimer];
}
- (void)layoutSubviews
{
[super layoutSubviews];
// collectionView
self.collectionView.frame = self.bounds;
// layout
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
layout.itemSize = self.bounds.size;
}
#pragma mark - 定时器
- (void)startTimer
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)stopTimer
{
[self.timer invalidate];
self.timer = nil;
}
- (void)nextPage
{
CGPoint offset = self.collectionView.contentOffset;
offset.x += self.collectionView.frame.size.width;
[self.collectionView setContentOffset:offset animated:YES];
}
#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return XMGItemCount * self.images.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
XMGImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:XMGImageCellId forIndexPath:indexPath];
cell.imageView.image = self.images[indexPath.item % self.images.count];
return cell;
}
#pragma mark - 其他
/**
* 重置cell的位置到中间
*/
- (void)resetPosition
{
// 滚动完毕时,自动显示最中间的cell
NSInteger oldItem = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
NSInteger newItem = (XMGItemCount * self.images.count / 2) + (oldItem % self.images.count);
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:newItem inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}
#pragma mark - <UICollectionViewDelegate>
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 停止定时器
[self stopTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// 开启定时器
[self startTimer];
}
/**
* scrollView滚动完毕的时候调用(通过setContentOffset:animated:滚动)
*/
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self resetPosition];
}
/**
* scrollView滚动完毕的时候调用(人为拖拽滚动)
*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self resetPosition];
}
@end
调用
- (void)viewDidLoad {
[super viewDidLoad];
XMGInfiniteScrollView *scrollView = [[XMGInfiniteScrollView alloc] init];
scrollView.images = @[
[UIImage imageNamed:@"img_00"],
[UIImage imageNamed:@"img_01"]
];
scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, 200);
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
}
分页显示
// 3.设置总页数
self.pageControl.numberOfPages = count;
// 4.单页的时候是否隐藏pageControl
self.pageControl.hidesForSinglePage = YES;
// if (count <= 1) {
//// self.pageControl.alpha = 0;
// self.pageControl.hidden = YES;
// }
// 5.设置pageControl图片(KVC)
[self.pageControl setValue:[UIImage imageNamed:@"current"] forKeyPath:@"_currentPageImage"];
[self.pageControl setValue:[UIImage imageNamed:@"other"] forKeyPath:@"_pageImage"];