2011-08-05 6 views
1

J'ai 12 images, stockées dans un tableau ...Comment randomiser des images dans une page scrollview

Je l'utilise pour sortir l'image.

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] 

Comment puis-je produire des images aléatoires dans l'UIView? comme il y a 12 images, mais chaque fois que je lance l'application, l'application va commencer à une image aléatoire, mais je serai toujours en mesure de faire défiler les images. J'espère que vous comprenez ma question.

Répondre

3

Vous pouvez « shuffle » la NSMutableArray chaque fois que vous créez:

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

Donc, chaque fois que vous serez le UIScrollView avec initialisez un ordre différent.

shuffle ne fait pas partie du SDK. Pour un exemple d'implémentation, s'il vous plaît 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 

Importer le fichier d'en-tête vous contenant la déclaration de la catégorie:

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

où vous souhaitez utiliser shuffle.

+0

ah, c'est gentil ... Merci ... mais quand j'ai essayé le code, il dit que shuffle ne répond pas à NSMutableArray. – Harvin

+0

L'avertissement ne devrait poser aucun problème. Quoi qu'il en soit, voir mon édition pour savoir comment supprimer cela. – sergio

+0

avez-vous eu du succès avec ça? – sergio

Questions connexes