简体   繁体   中英

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.

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.

I recommend you to use array directly like this.

NSArray *images = [[NSArray alloc] initWithObjects:
                 [UIImage imageNamed:@"1.jpg"],
                 [UIImage imageNamed:@"2.jpg"],
                 nil];

self.loadingView.animationImages = images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];

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.

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