Layout:
.h
#import <UIKit/UIKit.h>
@interface FlowLayout : UICollectionViewFlowLayout
@end
.m
#import "FlowLayout.h"
@implementation FlowLayout
- (void)prepareLayout{
[super prepareLayout];
self.itemSize = self.collectionView.bounds.size;
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.minimumLineSpacing = 0;
self.minimumInteritemSpacing = 0;
self.collectionView.showsVerticalScrollIndicator = NO;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.bounces = NO;
self.collectionView.pagingEnabled = YES;
}
@end
CollectionView
.h
#import <UIKit/UIKit.h>
@interface CollectionView : UICollectionView
- (instancetype)initWithUrls:(NSArray <NSURL *>*)urls;
@end
.m
#import "CollectionView.h"
#import "CollectionViewCell.h"
#import "FlowLayout.h"
static NSString * const reuseIdentifier = @"reuseIdentifier";
@interface CollectionView () <UICollectionViewDataSource,UICollectionViewDelegate>
@end
@implementation CollectionView{
NSArray <NSURL *>*_urlArray;
}
- (instancetype)initWithUrls:(NSArray <NSURL *>*)urls{
self = [super initWithFrame:CGRectZero collectionViewLayout:[[FlowLayout alloc]init]];
if (self) {
_urlArray = urls;
[self registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
self.dataSource = self;
self.delegate = self;
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_urlArray.count inSection:0];
[self scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:NO];
});
}
return self;
}
#pragma mark --UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _urlArray.count * 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.url = _urlArray[indexPath.item % _urlArray.count];
return cell;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGFloat contentOffset = scrollView.contentOffset.x;
NSInteger currentImg = contentOffset / scrollView.bounds.size.width;
if (currentImg == 0) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_urlArray.count inSection:0];
[self scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:NO];
}
if (currentImg == [self numberOfItemsInSection:0] - 1) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_urlArray.count - 1 inSection:0];
[self scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:NO];
}
}
@end
在自定义的构造函数中,手动滚动CollectionView时,之所以使用了主队列异步,是由于构造函数在数据源方法前执行,使用主队列异步,保证数据源方法执行完毕后,再滚动CollectionView
CollectionCell
.h
#import <UIKit/UIKit.h>
#import "CollectionView.h"
@interface CollectionViewCell : UICollectionViewCell
@property (nonatomic,strong) NSURL *url;
@end
.m
#import "CollectionViewCell.h"
#import "Masonry.h"
@interface CollectionViewCell()
@property (nonatomic,strong) NSOperationQueue *queue;
@end
@implementation CollectionViewCell{
UIImageView *_imageView;
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"%s",__FUNCTION__);
_imageView = [[UIImageView alloc]initWithFrame:self.bounds];
[self.contentView addSubview:_imageView];
}
return self;
}
- (void)setUrl:(NSURL *)url{
_url = url;
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
dispatch_sync(dispatch_get_main_queue(), ^{
_imageView.image = image;
});
}];
[self.queue addOperation:operation];
}
- (NSOperationQueue *)queue{
if (_queue == nil) {
_queue = [[NSOperationQueue alloc] init];
}
return _queue;
}
@end
将CollectionView分别抽取出CollectionView,Layout和Cell三个类,让代码的可读性更好,当外界使用使用:
#import "ViewController.h"
#import "CollectionView.h"
#import "Masonry/Masonry.h"
@interface ViewController ()
@end
@implementation ViewController{
NSArray *_urlArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData];
self.view.backgroundColor = [UIColor whiteColor];
CollectionView *collectionView = [[CollectionView alloc]initWithUrls:_urlArr];
[self.view addSubview:collectionView];
[collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view).mas_offset(20);
make.top.mas_equalTo(self.view).mas_offset(100);
make.right.mas_equalTo(self.view).mas_offset(-20);
make.height.mas_equalTo(300);
}];
}
- (void)loadData{
NSMutableArray *mArr = [NSMutableArray array];
for (int i = 0; i<3; i ++) {
NSString *urlString = @"http://img2.duitang.com/uploads/item/201208/18/20120818150713_zarnG.jpeg";
[mArr addObject:[NSURL URLWithString:urlString]];
}
_urlArr = mArr.copy;
}
@end