To make an photo gallery in iphone
On the iPhone I would like to display 13 image at a time, with 4 of them on each row. I've managed to get the first row with 4 images, but I'm having trouble with the rest. Here is what I have so far:
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];
How do I get the rest of the images to show?
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];
}
The key here is that whenever i
equals to 3 modulo 4, you simply move startPoint
to the beginning of the next line.
精彩评论