用代码实现,如图布局(object-c写的)
- (void)initButtons{
int level = 1;
int x0 = 10;
int y0 = 108;
int x = 60;
int y = 100;
int j = 0;
for (int i=1; i<16; i++) {
j = i-1;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x0+(j%5)*x, y0+(j/5)*y, 50, 50)];
button.tag = i;
if (i<=level) {
[button setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
}else{
[button setBackgroundImage:[UIImage imageNamed:@"lock.png"] forState:UIControlStateNormal];
}
[button addTarget:self action:@selector(playView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button release];
}
}

int level = 1;
int x0 = 10;
int y0 = 108;
int x = 60;
int y = 100;
int j = 0;
for (int i=1; i<16; i++) {
j = i-1;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
// CGRectMake(x0+(j%5)*x, y0+(j/5)*y, 50, 50)];
button.tag = i;
button.frame = CGRectMake(x0+(j/5)*320+ x*(j%5), y0, 50, 50);
if (i<=level) {
[button setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
}else{
// [button setBackgroundImage:[UIImage imageNamed:@"lock.png"] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor greenColor]];
}
[button addTarget:self action:@selector(playView:) forControlEvents:UIControlEventTouchUpInside];
[_myScrollview addSubview:button];
[button release];
}



本文介绍了一个使用Objective-C编写的布局代码实例,通过循环创建并排列了15个按钮,展示了如何根据条件设置按钮的背景图片及位置。
&spm=1001.2101.3001.5002&articleId=8717161&d=1&t=3&u=9b7628a3f13b4e07889fd62ad7bdb165)
3338

被折叠的 条评论
为什么被折叠?



