在iPhone上,我想一次显示13张图像,每行显示4张。我已经设法得到了4个图像的第一排,但我在其他方面遇到了麻烦
NSInteger startPoint = 10;
for (int i=0; i<13; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[self getImageFromName:@"headshotsmile"] forState:UIControlStateNormal];
btn.frame = CGRectMake(startPoint, 10, 40, 40);
startPoint = startPoint + 40 + btn.frame.size.width;
[self.view addSubview:btn];
如何显示其余图像?
CGPoint startPoint = CGPointMake(10, 10);
for (int i = 0; i < 13; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[self getImageFromName:@"headshotsmile"] forState:UIControlStateNormal];
btn.frame = CGRectMake(startPoint.x, startPoint.y, 40, 40);
startPoint.x += 40 + btn.frame.size.width;
if (i % 4 == 3) {
startPoint.x = 10;
startPoint.y += 40 + btn.frame.size.height;
}
[self.view addSubview:btn];
}
这里的关键是,每当i
等于3模4时,只需将startPoint
移动到下一行的开头。