Multiple image in scrollview

2019-09-06 13:46发布

I have 12 images and i want to display this images in UIScrollview with 4 rows where each rows have 3 images.

3条回答
Summer. ? 凉城
2楼-- · 2019-09-06 14:24
x=0;
y=0;
 UIScrollView  *scroller=[[UIScrollView alloc]init];
          scroller.frame=CGRectMake(20.0, 10.0, 250.0, 250.0);
       //  WithFrame:CGRectMake(6, 0, 250, 250)];
scroller.delegate=self;

[self.view addSubview:scroller];

[scroller setContentSize:(CGSizeMake(300,(([mainArray count]+3-1)/3)*20))];

  //main array means ur image array
for (int i=1; i<[mainArray count]+1; i++)
{
    if (x%3==0)
    {
        x=0;
        y++;
    }

    UIImageView *imgFirstRowActivitySelector=[[UIImageView alloc]initWithFrame:CGRectMake(x*60, y*60, 50, 50)];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{


        //    imgFirstRowActivitySelector.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[mainArray objectAtIndex:i-1]]]]];

            or
        //  imgFirstRowActivitySelector.image=[UIImage imagenamed:@"%@",[mainArray objectAtIndex:i-1]];


            imgFirstRowActivitySelector.userInteractionEnabled=YES;

            imgFirstRowActivitySelector.contentMode = UIViewContentModeScaleToFill;
            imgFirstRowActivitySelector.tag=i-1;

        });
    });
    [scroller addSubview:imgFirstRowActivitySelector];
    imgFirstRowActivitySelector=nil;
    x++;
}
查看更多
Explosion°爆炸
3楼-- · 2019-09-06 14:42

Try to use UICollectionView it is subclass of UIScrollView.

查看更多
女痞
4楼-- · 2019-09-06 14:43

It is possible to display 12 images in scroll view but I would suggest you to use UICollectionView. It uses same concept like UITableView and will eliminate task of managing frame of each image.

查看更多
登录 后发表回答