标签:
目的:无限图片轮播
技术:UICollectionView
技术要点:利用UICollectionViewCell的重用实现无限图片轮播,实例提供5张图片
主控制器代码
// ViewController.m
// UICollectionView-无限轮播-demo
#import "ViewController.h"
#import "BQAdvCell.h"
#import "BQBeauty.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSArray *beauties;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong,nonatomic) NSTimer *timer;
@end
@implementation ViewController
-(NSArray *)beauties{
if (_beauties == nil) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"beauties" ofType:@"plist"];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *array = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
BQBeauty *beauty = [BQBeauty beautyWithDict:dict];
[array addObject:beauty];
}
_beauties = array;
}
return _beauties;
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置代理
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
//注册cell
[self.collectionView registerNib:[UINib nibWithNibName:@"BQAdvCell" bundle:nil]forCellWithReuseIdentifier:@"beauty"];
//默认选择5000组
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:50 inSection:0]atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
//设置pageControl
//1.总页数
self.pageControl.numberOfPages = self.beauties.count;
//定时器
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(bro) userInfo:nil repeats:YES];
self.timer = timer;
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(void)bro{
static int index = 0;
if (index == 5) {
index = 0;
}
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:50+index inSection:0]atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
index++;
}
#pragma mark - UICollectionViewDataSource
//返回多少组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
//返回多少个
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.beauties.count * 100;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//1.去缓存池取cell
BQAdvCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"beauty" forIndexPath:indexPath];
//2.设置cell
cell.beauty = self.beauties[indexPath.item % 5];
//3.返回cell
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page = scrollView.contentOffset.x / 300;
self.pageControl.currentPage = page % 5;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//关闭定时器
[self.timer setFireDate:[NSDate distantFuture]];
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
//开启定时器
[self.timer setFireDate:[NSDate distantPast]];
}
标签:
原文地址:http://www.cnblogs.com/newFansOne/p/4824384.html