0

我发现这段代码对于避免第一次动画延迟非常有帮助:

NSMutableArray *menuanimationImages = [[NSMutableArray alloc] init];

for (int aniCount = 1; aniCount < 21; aniCount++) {
    NSString *fileLocation = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @"bg%i", aniCount + 1] ofType: @"png"];
    // here is the code to pre-render the image
    UIImage *frameImage = [UIImage imageWithContentsOfFile: fileLocation];
    UIGraphicsBeginImageContext(frameImage.size);
    CGRect rect = CGRectMake(0, 0, frameImage.size.width, frameImage.size.height);
    [frameImage drawInRect:rect];
    UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [menuanimationImages addObject:renderedImage];
}

settingsBackground.animationImages = menuanimationImages;

但我有时想使用相同的图像 - 我可以通过手动填写我的数组上方的代码吗

testArray  = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"test_001.png"],


                           [UIImage imageNamed:@"test_002.png"],

                           [UIImage imageNamed:@"test_001.png"],
nil];

感谢您的想法!

4

1 回答 1

0

是的,你可以使用这个

UIImageView * animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200,200)];

testArray  = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"test_001.png"], [UIImage imageNamed:@"test_002.png"],[UIImage imageNamed:@"test_001.png"],nil];
animTime =3.0;
[animatedImageView setAnimationImages:testArray] ;
animatedImageView.animationDuration =  animTime;
animatedImageView.animationRepeatCount = 1;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];
于 2012-11-13T13:38:54.503 回答