UImage 视图动画在我的 iPhone 上不起作用

UImage view animation it doesn't work on my iPhone

生成代码后,我的手机上没有任何动画

但是代码看起来不错,我不知道怎么弄,

而我的图像是 829 * 445,是否会导致此问题?

谁能帮我解决这个问题,我将不胜感激,谢谢

#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

您没有将 imageView 添加到 ViewController 视图。

-viewDidLoad

中尝试 [self.view addSubview:imageView];

您似乎没有将动画 UIImageView 添加到视图层次结构中。在 [imageView startAnimating]; 行之前添加以下代码行:

[self.view addSubview:imageView];

请使用动画块代码代替旧方法。动画会告诉您何时完成。您不需要设置定时器。

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");
    }];