参考链接
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat x = 0.0f;
CGFloat y = 60.0f;
CGFloat width = self.view.frame.size.width;
CGFloat height = 200.0f;
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.minimumLineSpacing = 10.0f;
_flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_flowLayout.itemSize = CGSizeMake(width - 50.0f, height - 20.0f);
// _flowLayout.sectionInset = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(x, y, width, height) collectionViewLayout:_flowLayout];
_collectionView.backgroundColor = [UIColor orangeColor];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.pagingEnabled = NO;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionCellIdentifier];
[self.view addSubview:_collectionView];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 20;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0f];
return cell;
}
- 关键代码 UIScrollViewDelegate的一个方法
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
if (![scrollView isKindOfClass:[UICollectionView class]]) {
return;
}
UICollectionView *collectionView = (UICollectionView *)scrollView;
CGPoint originalTargetContentOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y);
CGPoint targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(collectionView.bounds)/2, CGRectGetHeight(collectionView.bounds) / 2);
NSIndexPath *indexPath = nil;
while (indexPath == nil) {
targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(collectionView.bounds)/2, CGRectGetHeight(collectionView.bounds) / 2);
indexPath = [collectionView indexPathForItemAtPoint:targetCenter];
}
// 这里用attributes比用cell要好很多,因为cell可能因为不在屏幕范围内导致cellForItemAtIndexPath返回nil
UICollectionViewLayoutAttributes *attributes = [collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
if (attributes) {
*targetContentOffset = CGPointMake(attributes.center.x - CGRectGetWidth(collectionView.bounds)/2, originalTargetContentOffset.y);
} else {
NSLog(@"center is %@; indexPath is {%@, %@}; cell is %@",NSStringFromCGPoint(targetCenter), @(indexPath.section), @(indexPath.item), attributes);
}
NSLog(@"indexPath.row - %ld",(long)indexPath.row);
}