Display images in sequence with a loop

I have an NSArray with 5 pictures. I want to display them one by one in a loop. My code is:

 NSArray *tabImage = [[NSArray alloc] init];

tabImage = [[NSArray arrayWithObjects:  
               [UIImage imageNamed:@"picto_1.png"],
               [UIImage imageNamed:@"picto_2.png"],
               [UIImage imageNamed:@"picto_3.png"],
               [UIImage imageNamed:@"picto_4.png"],
               [UIImage imageNamed:@"picto_5.png"],
               nil] retain];

int var = 0;
var = indexPath.row;

for (int var = 0; var < 20; var++) {
    pictoImage.image = [tabImage objectAtIndex:(var % 5)];
}

I was getting the same picture all the time.

Thanks for the help.

+3
source share
4 answers

Im guessing pictoImageis this UIImageView. In this case, consider the class reference , in particular the properties animationImages.

 pictoImage.animationImages = [NSArray arrayWithObjects:  
                                   [UIImage imageNamed:@"picto_1.png"],
                                   [UIImage imageNamed:@"picto_2.png"],
                                   [UIImage imageNamed:@"picto_3.png"],
                                   [UIImage imageNamed:@"picto_4.png"],
                                   [UIImage imageNamed:@"picto_5.png"],
                                   nil];

 // How many seconds it should take to go through all images one time.
 pictoImage.animationDuration = 5.0;

 // How many times to repeat the animation (0 for indefinitely).
 pictoImage.animationRepeatCount = 4;

 [pictoImage startAnimating];
+6
source

You do not give the framework time to render images in a window.

All of your updates are combined into a single setNeedsDisplay call. At the next iteration of the run loop, the image will be displayed using the last image that you set in the loop.

+3

pictoImage UIImageView, @Hagelin - . , pictoImage UIImageView, , .

NSTimer , pictoImage tabImages. : -

- (void)setup {
    ...

    [NSTimer scheduledTimerWithTimeInterval:2.0
                                     target:self
                                   selector:@selector(updatePictoImage:)
                                   userInfo:nil
                                    repeats:YES];

    ...
}

- (void)updatePictoImage:(NSTimer*)theTimer {
    static int i = 0;
    if ( i > 20 ) {
        pictoImage.image = [tabImages objectAtIndex:(i % 5)];
    } else {
       [theTimer invalidate];
    }
}

pictoImage 2 tabImages.

+2

You are not printing anything.

You redefine the contents of yours pictoImage20 times in a row, leaving it with the last assignment when var is 19, so it should be picto_4.png.

In addition, the whole declaration vardoes not make much sense ... first you set it to 0, then set it to indexPath.row, and in the loop it is completely redefined ...

0
source

All Articles