Multiple image in scrollview

2019-09-06 14:26发布

问题:

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

回答1:

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++;
}


回答2:

Try to use UICollectionView it is subclass of UIScrollView.



回答3:

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.