如何为一组图像制作动画?

Baz*_*nga 0 iphone

如何动画一组图像,但我有一大套图像,确切地说是300,

 Background.animationImages = [NSArray arrayWithObjects:    
                                  [UIImage imageNamed:@"001.jpg"],
                                  [UIImage imageNamed:@"002.jpg"],
                                  [UIImage imageNamed:@"003.jpg"],
                                  [UIImage imageNamed:@"004.jpg"],
                                  [UIImage imageNamed:@"005.jpg"],
                                  [UIImage imageNamed:@"006.jpg"],
                                  [UIImage imageNamed:@"007.jpg"],
                                  [UIImage imageNamed:@"008.jpg"],
                                  [UIImage imageNamed:@"009.jpg"],
                                  [UIImage imageNamed:@"010.jpg"],
                                  nil];
Run Code Online (Sandbox Code Playgroud)

而且我不打算通过打字一直到300,这有解决方案吗?我可以使用循环吗?

Pau*_*l.s 5

循环可能看起来像这样

NSMutableArray *animationImages = [[NSMutableArray alloc] initWithCapacity:300];

for (int i = 1; i <= 300; i++) {
    NSString *imageName = [NSString stringWithFormat:@"%03d.jpg", i];
    [animationImages addObject:[UIImage imageNamed:imageName]];
}

Background.animationImages = animationImages;
Run Code Online (Sandbox Code Playgroud)

格式说明符%03d表示用0填充的整数,以使最小字段宽度为3.

根据您的图像大小,您可能会耗尽内存并终止您的应用.


边注

Background对于Objective-C上的变量,它不是一个非常好的名称.惯例是用小写字母开始变量名,然后使用驼峰大小写.