单独使用collectionCell视图
2015-10-12 20:00
579 查看
偶然间在别人工程中看到的,方法很简单下面就让我来给大家说下 简单实现使用collectionCell 创建等同于view的视图
1.自定义collectionCell 添加你需要的属性
2.自定义一个view 声明一个方法
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define floatByScreenWidth(a) ((a)/320.0)*SCREEN_WIDTH
- (void)setView;
实现:
- (void)setView//配置cell
{
for (int i = 0; i < 8; i++) {
MyCollectionCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"MyCollectionCell" owner:self options:nil]lastObject];
cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:arc4random()%255/255.0];
cell.nametitle.text = [NSString stringWithFormat:@"第%dcell",i];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
[tap addTarget:self action:@selector(ClickVideoSites:)];
cell.tag = i+1;
tap.view.tag = i+1;
[cell addGestureRecognizer:tap];
int x = i/8;
if (x<4) {
cell.frame = [self setFrameBynum:i andPage:x];
}else{
cell.frame = [self setFrameBynum:i andPage:x];
}
[self addSubview:cell];
}
}
//布局cell的位置
-(CGRect)setFrameBynum:(int)num andPage:(int)page
{
CGFloat length = floatByScreenWidth(74);
CGFloat height = length/74*85;
CGFloat width = (SCREEN_WIDTH-4*length)/5;
if (num < 4) {
return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*num, 0, length, height);
}
else
return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*(num-4),height + 10, length, height);
}
3.Controller里面使用
_myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
[_myView setView];
[self.view addSubview:_myView];
1.自定义collectionCell 添加你需要的属性
2.自定义一个view 声明一个方法
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define floatByScreenWidth(a) ((a)/320.0)*SCREEN_WIDTH
- (void)setView;
实现:
- (void)setView//配置cell
{
for (int i = 0; i < 8; i++) {
MyCollectionCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"MyCollectionCell" owner:self options:nil]lastObject];
cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:arc4random()%255/255.0];
cell.nametitle.text = [NSString stringWithFormat:@"第%dcell",i];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
[tap addTarget:self action:@selector(ClickVideoSites:)];
cell.tag = i+1;
tap.view.tag = i+1;
[cell addGestureRecognizer:tap];
int x = i/8;
if (x<4) {
cell.frame = [self setFrameBynum:i andPage:x];
}else{
cell.frame = [self setFrameBynum:i andPage:x];
}
[self addSubview:cell];
}
}
//布局cell的位置
-(CGRect)setFrameBynum:(int)num andPage:(int)page
{
CGFloat length = floatByScreenWidth(74);
CGFloat height = length/74*85;
CGFloat width = (SCREEN_WIDTH-4*length)/5;
if (num < 4) {
return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*num, 0, length, height);
}
else
return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*(num-4),height + 10, length, height);
}
3.Controller里面使用
_myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
[_myView setView];
[self.view addSubview:_myView];
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- maven使用经验集
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- 肯特·贝克:改变人生的代码整理魔法
- 你应该学习哪种编程语言?
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- [转]我们需要一种其他人能使用的编程语言
- 我的iPhone桌面
- DB2编程序技巧(1)
- DB2编程序技巧 (四)
- 女人VS编程_国庆快乐
- DB2编程序技巧 (六)
- DB2编程序技巧 (三)
- DB2编程序技巧 (九)