如何显示多个UIImageViews [英] How to display multiple UIImageViews
问题描述
我想在视图中随机显示5张图片。我的图像名称如下:image-0,image-1,image-2,image-3,image-4。
I have 5 images I would like to display on the view randomly. My image names are as follows: image-0, image-1, image-2, image-3, image-4.
我需要的是我想要显示每个图像,但问题是当我编写代码以显示第二个图像时,它会堆叠在第一个图像的顶部。这是我的代码显示我的一个图像:
What I need is I want to display each image, but the problem is when I write the code to display a second, it stacks on top of the first image. Here is my code to display one of my images:
- (void)viewDidLoad
{
int xValue = arc4random() % 320;
int yValue = arc4random() % 480;
image.center = CGPointMake(xValue, yValue);
{UIImageView *imgFive = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image-5.png"]];
[self.view addSubview:imgFive];
imgFive.center = CGPointMake(xValue, yValue);}
但如果我添加相同的代码,而不是image-5.png我使用其他文件名来尝试显示它们,它们堆叠在彼此之上。我的图像是圆形,每个都有不同的大小。因此,当我尝试显示所有5个时,我可以看到我的圆圈从最小到最大堆叠在彼此的顶部。
But if I add the same code but instead of "image-5.png" I use the other file names to try and display them, they stack on top of eachother. My images are circles, each varying size. So when I try and display all 5 I can see my circles have stacked on top of eachother from smallest to largest.
我需要做些什么才能将这些图像彼此分开?我是否需要将这些图像放在一个数组中然后循环遍历数组以显示它我想要的方式?
What do I need to do to separate these images from eachother? Do I need to put these images in an array and then loop through the array to display them how I would like?
感谢任何帮助,我对编码很新,所以一个简单的解释将有助于解释为什么你的代码有效。
Any help is appreciated, I am very new to coding so a simple explanation would be helpful in explaining why your code works.
再次感谢!
推荐答案
考虑您的要求,以及一般情况提供给你的图像的结构名称,我想你需要循环你的逻辑。
Considering your requirement, and the general structure names provided to your images, i guess you need to just loop your logic .
for(int i=0;i<5;i++) {
float xpos = arc4random() %320 ;
float ypos = arc4random() %480 ;
UIImage *image =
[UIImage imageNamed:[NSString stringWithFormat:@"image-%d.png",i]] ;
UIImageView *imageView =
[[UIImageView alloc]initWithImage:image];
imageView.center=CGPointMake(xpos, ypos);
[self.view addSubview:imageView];
}
这篇关于如何显示多个UIImageViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!