偶然间在别人工程中看到的,方法很简单下面就让我来给大家说下 简单实现使用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];
}
}
-(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);
}
_myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
[_myView setView];
[self.view addSubview:_myView];