Search code examples
iosobjective-canimationuiimageview

UIImageView won't animate, static image only


I have a UIImageView on my storyboard, which is linked to an outlet in the code. When I run the code, it only shows the static picture that the storyboard shows, not the animation that I create programmatically. Any ideas?

for(int i=0; i<4; i++) {
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading%d",i]];
    [self.images addObject:image];
}
self.loadingView.animationImages = self.images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];

Note: loadingView is the UIImageView, images is an NSMutableArray.


Solution

  • If you want to make run time array you can use like this way.

      NSMutableArray *images = [[NSMutableArray alloc] init];
    
        for (int i = 1 ; i < 4; i++) {
    
            [images addObject:[UIImage imageNamed:[NSString stringWithFormat: @"loading%d.jpg", i]]];
    
    
        }
    
        self.loadingView.animationImages = images;
        self.loadingView.animationDuration = 2;
        self.loadingView.animationRepeatCount = 1;
        [self.loadingView startAnimating];
    

    Hope this will help you. It's working fine for me.