所以在我的轮播中我实现了这个:
- (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]];
}
最佳答案
你可以试试这个:首先你用你的代码填充你的 images
数组。然后您洗牌这个有序数组。这里有一个很好的 NSArray 洗牌类别:Shuffling an NSArray .
请注意,您应该删除 NSArray+Helpers.h
中的空 #import 行
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;
}
关于iphone - 随意混搭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10733108/