简体   繁体   中英

Display random image in UIImageView animation

In the normal way for showing animations, we provide a sequence of images which are shown one after the other. Something like:

NSArray *images1 = [[NSArray alloc] initWithObjects:
                                  [UIImage imageNamed:@"img1.png"],
                                  [UIImage imageNamed:@"img2.png"],
                                  [UIImage imageNamed:@"img3.png"],
                                  [UIImage imageNamed:@"img4.png"],
                                  nil];

        images1=[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 256.0, 256.0)];
        images1.transform = rotateTransform1;
        images1.animationImages = myDustImages1;
        images1.animationDuration = 0.2; // seconds
        images1.animationRepeatCount = 0; // 0 = loops forever
        [images1 startAnimating];

Is there any way I can show a randomly picked image from these four (or any number of) images? Currently I am thinking of something like

NSArray *images1 = [[NSArray alloc] initWithObjects:
                                  [UIImage imageNamed:@"img1.png"],
                                  [UIImage imageNamed:@"img2.png"],
                                  [UIImage imageNamed:@"img3.png"],
                                  [UIImage imageNamed:@"img4.png"],

                                  [UIImage imageNamed:@"img2.png"],
                                  [UIImage imageNamed:@"img3.png"],
                                  [UIImage imageNamed:@"img4.png"],
                                  [UIImage imageNamed:@"img1.png"],

                                  [UIImage imageNamed:@"img3.png"],
                                  [UIImage imageNamed:@"img4.png"],
                                  [UIImage imageNamed:@"img2.png"],
                                  [UIImage imageNamed:@"img1.png"],
                                  nil];..........

which is not random at all, but won't look repetitive to most people, especially with only 0.2 seconds separating the current and the next image.

Is there any way I can make it better?

People are remarkably good at seeing patterns. If you do want repetition, then at least base it on prime numbers, since those are much harder to spot.

In any case, you could do this yourself simply by setting up a timer and randomly selecting a new image each time the timer ticks. If you want to ensure you don't show the same image twice in a row, the simplest way is to just randomly pick an image from your array until you get a different one. If you want to ensure you show all 4 images before showing one of them twice, then randomly shuffle your array and take items off the front, then when you run out, shuffle it again (and to avoid the duplicate problem, just keep shuffling until the first entry in the array is different than the last entry in the previous array).

-(NSArray *)shuffledArray
{

 NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];

 NSMutableArray *copy = [self mutableCopy];
 while ([copy count] > 0)
 {
  int index = arc4random() % [copy count];
  id objectToMove = [copy objectAtIndex:index];
  [array addObject:objectToMove];
  [copy removeObjectAtIndex:index];
 }

   [copy release];
   return array;
}

Try something like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM