//
// ViewController.m
// 12.14 采集试图 UICollection
//
// Created by DC017 on 15/12/14.
// Copyright © 2015年 DC017. All rights reserved.
//
#import "ViewController.h"
ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
UICollectionView * collection;
//重用标示符
NSString * CellIdenntifier;
float f;
}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CellIdenntifier=@"标示";
[self layout];
f=0;
}
-(void)layout{
UICollectionViewFlowLayout *bujufangan=[[UICollectionViewFlowLayout alloc]init];
//设置滚动方向(垂直或水平)
//翻页
//collection.pagingEnabled=YES;
collection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, 375, 647) collectionViewLayout:bujufangan];
collection.backgroundColor=[UIColor orangeColor];
//bujufangan.scrollDirection=//滚动方向
//水平滚动
bujufangan.scrollDirection=UICollectionViewScrollDirectionHorizontal;
[self.view addSubview:collection];
collection.delegate=self;
collection.dataSource=self;
//注册采集试图所使用的cell类,并设置标示符
[collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdenntifier];
}
#pragma mark 设置Ceel
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell=[collection dequeueReusableCellWithReuseIdentifier:CellIdenntifier forIndexPath:indexPath];
cell.backgroundColor=[UIColor colorWithWhite:(float)indexPath.row/(66*1.0) alpha:1];
//选中后变红色
// UIView* view=[[UIView alloc]initWithFrame:cell.bounds];
// view.backgroundColor=[UIColor redColor];
// cell.selectedBackgroundView=view;
return cell;
}
#pragma marc 设置单个section 里的cell
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 66;
}
#pragma mark 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%ld",indexPath.row);
}
#pragma mark 自定义cell 尺寸
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
f=f+2;
return CGSizeMake(f, f);
}
#pragma mark 定义每个section 的内边距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(100, 40, 0, 40);
}
#pragma mark 设定cell行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 600;
}
#pragma mark 设置列间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 20;
}
//交互
//1.手指按下某个cell
//2.shouldHighlightIemAtIndexPach(如果返回yes,则向下执行,否则终止);
//3.didHightlightItemAtIndexPath(高亮)
//4.手指松开
//5.didUnhighlghtItemAtIndexPath(取消高亮);
//6.shouldSelectItemAtIndexPath(如果返回yes,则继续向下执行,否则终止)
//7.didSelectItemAtIndexPath(执行选择事件)
-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
}
-(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
[collectionView cellForItemAtIndexPath:indexPath].backgroundColor = [UIColor whiteColor];
}
#pragma mark 页眉 页脚 水平width 垂直height
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(200,10 );
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end