开发者

To make an photo gallery in iphone

开发者 https://www.devze.com 2023-03-09 21:57 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消