相信,部分人用scrollerView做过,或者见过一组图片循环滚动的效果,还有一些,比较多的图片展示页面,这是如果还使用scrollerView的话,那么显然很不合理,比较麻烦,今天,就来介绍一种继承与UIScrollerView的控件,UICollertionView。
较简单的用法,后期会作践完善
ViewController.h 中
#import <UIKit/UIKit.h>
//此时,需要签署三个协议,代理,数据源 和 layout
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@end
// ViewController.m
// collection
//
// Created by Mac on 15-7-23.
// Copyright (c) 2015年 Mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化布局,创建layout,必须设置
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//设置内容滑动方向
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//设置垂直方向得最小距离
layout.minimumInteritemSpacing = 10;
//设置水平方向得最小距离
layout.minimumLineSpacing = 10;
//设置四周得边缘距离
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//创建collectionView,并添加layout,必须添加
UICollectionView *view = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
[self.view addSubview:view];
view.backgroundColor = [UIColor redColor];
//签署代理和数据源
view.delegate = self;
view.dataSource = self;
//注册单元格。 注册带xib的cell的方法不同
[view registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"newTtem"];
//组册组得头视图,可设置,可不设置
[view registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView"];
//组册组得尾视图,可设置,可不设置,若设置,则下面必须返回其大小
[view registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
}
注:若要显示组的头尾试图,需要返回layout方法里的头尾试图的大小。此处的头尾试图指的是组的头尾试图。-(CGSize)collection 开头的数据源代理。
//创建组得个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
//设置单元格的个数,在collectionView称其为item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 40;
}
//创建item得内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newTtem" forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
return cell;
}
//代理方法事项单元格得大小,单元格显示的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(50, 50);
}
//创建完cell得内容后,仍要设置代理方法,返回头视图,和尾视图
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headView" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor purpleColor];
return headerView;
}
UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerView" forIndexPath:indexPath];
footer.backgroundColor = [UIColor redColor];
return footer;
}
}
#define mark单元格得点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}
//设置初始时距左距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
//上面说过collectionView继承与ScrollerView,所以其代理方法也可使用
//设置单元格将要结束拖动时调用
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
}
@end