简体   繁体   中英

UImage view animation it doesn't work on my iPhone

After build code, it doesn't have any animation on my cellphone

but the code looks good, i don't know how to figure it out ,

and my image is 829 * 445, does it cause this problem ?

Could someone help me to solve this problem i will really appreciate it, thanks

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],
                                [UIImage imageNamed:@"2.jpg"],
                                [UIImage imageNamed:@"3.jpg"],
                                [UIImage imageNamed:@"4.jpg"],nil];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,150,150)];
    imageView.animationImages = animationImages ;
    imageView.animationRepeatCount = 2;
    imageView.animationDuration= 4.0;
    [imageView startAnimating];

    [NSTimer scheduledTimerWithTimeInterval:4.0 target:self
                                   selector:@selector(animationDone:)
                                   userInfo:nil repeats:NO];
} 

-(void)animationDone:(NSTimer*)inTimer
{
    [inTimer invalidate];
    inTimer = nil;
    NSLog(@"animationDone ");

}

@end

You don't added imageView to your ViewController view.

Try [self.view addSubview:imageView]; in -viewDidLoad

It looks like you're not adding the animating UIImageView to your view hierarchy. Right before your [imageView startAnimating]; line add the below line of code:

[self.view addSubview:imageView];

Please use animation block code instead of the older method. The animation will tell you when its done. You don't need to set a timer.

NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],
                                [UIImage imageNamed:@"2.jpg"],
                                [UIImage imageNamed:@"3.jpg"],
                                [UIImage imageNamed:@"4.jpg"],nil];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,150,150)];

    [UIView animateWithDuration:1.0f animations:^{

        for (int loop = 0; loop < 4; loop++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFornmat:@"%d.jpg", (loop + 1)]];
            [imageView setImage:image];
        }

    } completion:^(BOOL finished) {
         NSLog(@"animationDone");
    }];

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