Ios 如何在页面滚动视图中随机化图像
我有12个图像,存储在一个数组中 我用它来输出图像Ios 如何在页面滚动视图中随机化图像,ios,uiscrollview,uiimageview,Ios,Uiscrollview,Uiimageview,我有12个图像,存储在一个数组中 我用它来输出图像 scrollView = [[UIScrollView alloc] init]; CGRect scrollFrame; scrollFrame.origin.x = 0; scrollFrame.origin.y = 0; scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE; scrollFrame.size.height = HEIGHT_OF_SCROL
scrollView = [[UIScrollView alloc] init];
CGRect scrollFrame;
scrollFrame.origin.x = 0;
scrollFrame.origin.y = 0;
scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;
scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;
scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
NSMutableArray *slideImages = [[NSMutableArray alloc] init];
[slideImages addObject:@"KODAK1.png"];
[slideImages addObject:@"KODAK2.png"];
[slideImages addObject:@"KODAK3.png"];
[slideImages addObject:@"KODAK4.png"];
[slideImages addObject:@"KODAK5.png"];
[slideImages addObject:@"KODAK6.png"];
[slideImages addObject:@"KODAK7.png"];
[slideImages addObject:@"KODAK8.png"];
[slideImages addObject:@"KODAK9.png"];
[slideImages addObject:@"KODAK10.png"];
[slideImages addObject:@"KODAK11.png"];
[slideImages addObject:@"KODAK12.png"];
srandom(time(NULL));
int x = arc4random() % 12;
for ( int i = 0 ;i<[slideImages count]; i++) {
//loop this bit
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET, 0 , WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
}
[scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] +0), HEIGHT_OF_IMAGE)];
[scrollView setContentOffset:CGPointMake(0, 0)];
[self.view addSubview:scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:YES];
[super viewDidLoad]
scrollView=[[UIScrollView alloc]init];
CGRect滚动框;
scrollFrame.origin.x=0;
scrollFrame.origin.y=0;
scrollFrame.size.width=滚动页面的宽度;
scrollFrame.size.height=滚动页面的高度;
scrollView=[[UIScrollView alloc]initWithFrame:scrollFrame];
scrollView.bounces=是;
scrollView.PaginEnabled=是;
scrollView.showsHorizontalScrollIndicator=否;
scrollView.delegate=self;
scrollView.userInteractionEnabled=是;
NSMUTABLEARRY*SLIDEMAGES=[[NSMUTABLEARRY alloc]init];
[幻灯片图像添加对象:@“KODAK1.png”];
[幻灯片图像添加对象:@“KODAK2.png”];
[幻灯片图片添加对象:@“KODAK3.png”];
[幻灯片图片添加对象:@“KODAK4.png”];
[幻灯片图片添加对象:@“KODAK5.png”];
[幻灯片图片添加对象:@“KODAK6.png”];
[幻灯片图片添加对象:@“KODAK7.png”];
[幻灯片图片添加对象:@“KODAK8.png”];
[幻灯片图像添加对象:@“KODAK9.png”];
[幻灯片图像添加对象:@“KODAK10.png”];
[幻灯片图片添加对象:@“KODAK11.png”];
[幻灯片图片添加对象:@“KODAK12.png”];
srandom(时间(空));
int x=arc4random()%12;
对于(int i=0;i您可以在每次创建NSMutableArray时对其进行“洗牌”:
NSMutableArray *slideImages = [[NSMutableArray alloc] init];
...
[slideImages shuffle];
...
因此,每次您都将以不同的顺序初始化UIScrollView
shuffle
不是SDK的一部分。有关示例实现,请:
无论您想在哪里使用shuffle
啊,这很好…谢谢…但当我尝试该代码时,它说shuffle对NSMutableArray没有响应。警告应该没有问题。无论如何,请查看我的编辑以了解如何删除该警告。您是否成功地使用了该命令?我已经完成了此操作,现在正在准备一些图像…将更新此命令(很快就有人问:)对不起,上周末我正忙着上网呢
@implementation NSMutableArray (Shuffling)
- (void)shuffle
{
static BOOL seeded = NO;
if(!seeded)
{
seeded = YES;
srandom(time(NULL));
}
NSUInteger count = [self count];
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
int nElements = count - i;
int n = (random() % nElements) + i;
[self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
@end
@interface NSMutableArray (Shuffling)
- (void)shuffle;
@end