UICollectionViewController
UICollectionViewController层次结构:控制器View 上面UICollectionView
self.view ! = self.collectionView
1.初始化的时候必须设置布局参数,通常使用系统提供的流水布局UICollectionViewFlowLayout
2.cell必须通过注册
3.自定义cell
- (instancetype)init
{
// 流水布局对象,设置cell的尺寸和位置
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 设置滚动的方向
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// 设置cell的尺寸
layout.itemSize = XMGScreenBounds.size;
// // 设置cell之间间距
layout.minimumInteritemSpacing = 0;
// // 设置行距
layout.minimumLineSpacing = 0;
//
// // 设置每一组的内间距
// layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
return [super initWithCollectionViewLayout:layout];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView.bounces = NO;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.pagingEnabled = YES;
// 注册cell
[self.collectionView registerClass:[XMGNewFeatureCell class] forCellWithReuseIdentifier:ID];
[self setUpAllChildView];
}
- 减速完成时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// 获取当前x偏移量
CGFloat curOffsetX = scrollView.contentOffset.x;
// 获取差值
CGFloat delta = curOffsetX - _lastOffsetX;
_guideView.x += 2 * delta;
_guideLargetView.x += 2 * delta;
_guideSmallView.x += 2 * delta;
[UIView animateWithDuration:0.25 animations:^{
_guideView.x -= delta;
_guideLargetView.x -= delta;
_guideSmallView.x -= delta;
}];
int page = curOffsetX / self.view.width + 1;
// 修改控件的内容
_guideView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%d",page]];
_lastOffsetX = curOffsetX;
}
- 数据源方法
#pragma mark - 返回每个cell长什么样
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
XMGNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
NSString *imageName = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item + 1];
cell.image = [UIImage imageNamed:imageName];
// 告诉cell什么时候是最后一行
[cell setUpIndexPath:indexPath count:XMGPages];
return cell;
}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
WZShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:WZCellId forIndexPath:indexPath];
// 传递模型
cell.shop = self.shops[indexPath.item];
return cell;
}