2011-08-05 4 views
1

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の画像があるように、私はアプリケーションを実行するたびに、アプリはランダムな画像で開始しますが、私はまだ画像をスクロールすることができます。皆さん、私の質問を理解していただければ幸いです。

答えて

3

あなたは「シャッフル」はNSMutableArrayのは毎回あなたがそれを作成することができます。

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

ですから、異なる順序でUIScrollViewのを初期化されますたびに。

shuffleは、SDKの一部ではありません。サンプル実装については、have a look to thisください

@interface NSMutableArray (Shuffling) 
    - (void)shuffle; 
@end 

はどこにshuffleを使用したい:

@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 

インポートあなたのカテゴリの宣言を含むヘッダファイル。

+0

ああ、これはいいです...ありがと...しかし、私はコードを試してみると、シャッフルはNSMutableArrayに応答しないと言います。 – Harvin

+0

警告は問題ありません。とにかく、それを削除する方法について私の編集を参照してください。 – sergio

+0

これで成功しましたか? – sergio

関連する問題