在网格视图中从40个图像中随机显示25个图像 [英] To show 25 images randomly out of 40 images in a grid view
问题描述
我正在创建一个iphone应用,我需要在其中创建25个图像的网格视图。
我这样做是通过在一个数组中拍摄25张图像,并通过使用for循环显示它们,方法是在下面的代码中更改x轴和y轴的尺寸:
I am creating an iphone app in which I need to create a grid view of 25 images. I am doing that by taking 25 images in an array, and displaying them by using for loop by changing the dimensions for x axis and y axis in below code:
for(int i=0; i<25; i++)
{
if(i>0)
{
if(i%5==0)
{
xaxis=30;
yaxis=yaxis+35;
}
}
iconButton[i]=[UIButton buttonWithType:UIButtonTypeRoundedRect];
iconButton[i].frame=CGRectMake(xaxis, yaxis, 50, 30);
[iconButton[i] setBackgroundImage:[iconArray objectAtIndex:i] forState:UIControlStateNormal];
[iconButton[i] addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:iconButton[i]];
xaxis=xaxis+55;
}
它工作正常,但我和我共有40张图片,我每次都想要当应用程序启动时,它应该从25张图像中随机选取25张图像。
It working fine but I have total 40 images with me and I want every time when application will launch it should pick the 25 images randomly out of that 25 images.
我如何做到这一点,请帮助我。
How will I do that, please help me.
非常感谢您的帮助。
问候
iPhoneDeveloper11
Many Thanks in advance for your help. Regards iPhoneDeveloper11
推荐答案
创建一个包含41个数字(0-40)的数组,使用它们随机播放部分 Fisher-Yates shuffle
并使用数组的前25个元素。
Create an array of 41 numbers (0-40), shuffle them using a partial Fisher-Yates shuffle and use the first 25 elements of the array.
伪代码(random(x)返回0到x之间的随机数):
Pseudo-code (random(x) returns a random number from 0 to x inclusive):
array = [0, 1, 2, ..., 40]
for i in 0, 1, ..., 24 do
swap array[i], array[i + random(40 - i)]
truncate array to 25 elements.
这篇关于在网格视图中从40个图像中随机显示25个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!