UICollection 实现分页横向滚动到屏幕中央

参考链接

  • 创建一个UICollectionView
- (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);
    
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,145评论 1 32
  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 4,537评论 0 12
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,186评论 6 13
  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 3,926评论 1 22
  • 叶芽昨天拉了5次屎,今天到目前为止拉了4次。每次又多又臭,一屁股的屎比较难清理。熏得我和她爸都快受不了了。 叶芽是...
    青青叶芽阅读 208评论 0 0