如何从NSArray中选择随机图像并在视图上随机显示 [英] How to pick a random image from NSArray and display randomly on view

查看:185
本文介绍了如何从NSArray中选择随机图像并在视图上随机显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经做了几个线程询问如何随机显示图像/选择随机图像。我所认识到的是,我不知道如何将这两种方法结合在一起。

Okay well I have made a couple threads asking how to display images randomly/pick a random image. What I have came to realize is that I do not know how to combine these two methods together.

我在一个数组中有5张图片。

I have 5 images in an array.

我的.h文件:

@property (strong, nonatomic) NSArray *imageArray;

我的.m文件:

    - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

UIImage *image1 = [UIImage imageNamed:@"image-1.png"];
UIImage *image2 = [UIImage imageNamed:@"image-2.png"];
UIImage *image3 = [UIImage imageNamed:@"image-3.png"];
UIImage *image4 = [UIImage imageNamed:@"image-4.png"];
UIImage *image5 = [UIImage imageNamed:@"image-5.png"];

_imageArray = @[image1, image2, image3, image4, image5];
}

这就是我现在所拥有的,我一直在玩其他代码香港专业教育学院在其他地方发现,但没有成功,所以离开了。

This is what I have right now, I have been playing around with other code ive found elsewhere but have had no success so left it out.

现在您已经看到了我所拥有的,这就是我想要做的事情:
我需要使用一种方法来随机选择我的5张图片之一从我的数组中,然后在我的视图中随机显示它。

Now that you see what I have, this is what im trying to do: I need to use a method that will pick one of my 5 images randomly from my array and then display it in a random position in my view.

我还需要重复这个循环,但是有约束。我需要每个图像都有一个值,如:image-1等于1,图像-2等于2,图像-3等于3,图像-4等于4,图像-5等于5。重复循环,直到显示的图像等于总值50.

I also need to repeat this loop, but with constraints. I need each image to have a "value" such as: image-1 equal to 1, image-2 equal to 2, image-3 equal to 3, image-4 equal to 4, and image-5 equal to 5. And have the loop repeat until the images displayed equals a total value of 50.

我不确定从哪里开始使用哪种方法。我确定随机选择和随机显示对你们中的一些人很容易,但值和重复直到等于50似乎很复杂。所以非常感谢任何和所有的帮助!提前感谢任何可以提供帮助的人,我对编码很新,所以如果你能解释为什么使用你的代码,它会更有帮助!谢谢!

Im not really sure where to begin on what methods to use. Im sure picking and displaying randomly is easy to some of you but the values and repeating until equaling 50 seems complex. So any and all help is greatly appreciated! Thanks in advance to anyone who can help, im new to coding so if you can explain why you used your code it would help even more! Thanks!

编辑:这家伙试图帮助我在我的另一个线程中添加整个选择随机图像。他的回答如下:如何显示多个UIImageViews 我使用了他的代码但没有任何反应。我不确定他的代码是否在某处或者我做错了。

This guy tried helping me in my other thread adding the whole picking a random image as well. His response is here: How to display multiple UIImageViews and I used his code but nothing happened. I am not sure if his code is wrong somewhere or if I did something wrong.

推荐答案

使用递归循环和int来跟踪你所期望的50的进度。

Use a recursive loop and an int to keep track of your progress towards your desired count of 50.

每次循环,你都会想:


  1. 生成一个新的随机数(0-4)

  2. 使用随机数从阵列中选择一个图像

  3. 将你的随机数添加到你的int并检查你是否达到了50。

  4. 如果你已达到50,那么你就完成了

  5. 如果你没有达到50,那就再做一次。

  1. Generate a new random number (0-4)
  2. Use your random number to select an image from your array
  3. Add your random number to your int and check to see if you've hit 50.
  4. If you've hit 50, you're done
  5. If you haven't hit 50, do it again.

这样的事情:

//in your .h declare an int to track your progress
int myImgCount;

//in your .m
-(void)randomizeImages {

  //get random number
  int randomImgNum = arc4random_uniform(5);  

  //use your random number to get an image from your array
  UIImage *tempImg = [_imageArray objeactAtIndex:randomImgNum];

  //add your UIImage to a UIImageView and place it on screen somewhere
  UIImageView *tempImgView = [[UIImageView alloc] initWithImage:tempImg];  

  //define the center points you want to use
  tempImgView.center = CGPointMake(yourDesiredX,yourDesiredY);   

  [self addSubview:tempImgView];
  [tempImgView release];


  //increment your count
  myImgCount = myImgCount+(randomImgNum+1); 

  //check your count
  if (myImgCount<50) {
    [self randomizeImages];  //do it again if not yet at 50
  }

}

这样的事情对你有用。

Something like that should work for you.

这篇关于如何从NSArray中选择随机图像并在视图上随机显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆