gpt4 book ai didi

ios - 如何在页面 ScrollView 中随机化图像

转载 作者:行者123 更新时间:2023-12-01 19:26:41 26 4
gpt4 key购买 nike

我有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_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]

如何在UIView中输出随机图像?因为其中有12张图像,但是每次我运行该应用程序时,该应用程序都将以随机图像启动,但是我仍然可以滚动浏览这些图像。我希望你们能理解我的问题。

最佳答案

您可以在每次创建NSMutableArray时对其进行“洗牌”:

 NSMutableArray *slideImages = [[NSMutableArray alloc] init];
...
[slideImages shuffle];
...

因此,每次您将以不同的顺序初始化UIScrollView时。
shuffle不属于SDK。有关示例实现,请 have a look to this:
@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

您想在哪里使用 shuffle

关于ios - 如何在页面 ScrollView 中随机化图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6953533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com