1

所以在我的轮播中我实现了这个:

- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{   
   // int randomforIndex = arc4random()%[images count]+1;
        UIImageView *imageView  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[images objectAtIndex:index]]];
        return imageView;


}

我将我的图像分配给NSString. 每当带有图像的轮播在特定位置停止时,[images objectAtIndex:index]它都会执行相应的操作。但是当我使用这个randomforIndex零件时index,一切都变得混乱了。所以我的问题是如何在不随机化它们的值的情况下随机化图像。

这就是我使用 NSString 添加图像的方式:

for (int x=1; x<76;x++) {
        // add as NSString
        [images addObject:[NSString stringWithFormat:@"%d", x]];
            }
4

2 回答 2

0

您将不得不单独存储随机列表。制作另一个数组并将随机序列放入该数组中。然后,您可以将其用作数据源,并保持数字和图像之间的链接完好无损。

于 2012-05-24T07:57:14.530 回答
0

你可以试试这个:首先你images用你的代码填充你的数组。然后你打乱这个有序数组。这里有一个很好的 NSArray shuffle 类别:Shuffling an NSArray

请注意,您应该删除空的#import 行NSArray+Helpers.h

images = [[NSMutableArray alloc] init];
for (int x=1; x<76;x++)
{
    [images addObject:[NSString stringWithFormat:@"%d.png", x]];
}
images = (NSMutableArray *)[images shuffled];

这部分代码已经过测试并且可以正常工作。

从这一点开始,您应该可以使用简单的 viewForItemAtIndex 而不进行任何随机化,因为文件名已经被改组了。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{   
    if (view == nil)
    {
        view = [[UIImageView alloc] init];
    }

    UIImage* nimage = [UIImage imageNamed:[images objectAtIndex:index]];
    view.image = nimage;
    view.frame = CGRectMake(0.0,0.0,nimage.size.width,nimage.size.height);  

    return view;
}
于 2012-05-24T08:40:32.267 回答