通过交替使用不同的UIImages来制作动画

时间:2013-03-26 20:50:57

标签: ios xcode animation uiimageview uiimage

我有两个不同的图像,基本上只是彼此的反转。我想来回动画来创造一个有点移动的效果。在AppDelegate didFinishLaunching我有:

myImageWalk = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 568)];
    [self.myImageWalk setImage: [self loadingImage]];

    [window addSubview:myImageWalk];
    [window bringSubviewToFront:myImageWalk];

然后,同样在App Delegate中,我有:

-(UIImage*)loadingImage
{
    NSArray *animationFrames = [NSArray arrayWithObjects:
                                [UIImage imageNamed:@"original.png"],
                                [UIImage imageNamed:@"second.png"],
                                [UIImage imageNamed:@"original.png"],
                                [UIImage imageNamed:@"second.png"],
                                [UIImage imageNamed:@"original.png"],
                                [UIImage imageNamed:@"second.png"],
                                [UIImage imageNamed:@"original.png"],
                                [UIImage imageNamed:@"second.png"],
                                [UIImage imageNamed:@"original.png"],
                                [UIImage imageNamed:@"second.png"],
                                [UIImage imageNamed:@"original.png"],
                                [UIImage imageNamed:@"second.png"],
                                [UIImage imageNamed:@"original.png"],
                                [UIImage imageNamed:@"second.png"],
                                [UIImage imageNamed:@"original.png"],
                                [UIImage imageNamed:@"second.png"],
                                [UIImage imageNamed:@"original.png"],
                                [UIImage imageNamed:@"second.png"],
                                nil];
    return [UIImage animatedImageWithImages:animationFrames duration:6.0f];
}

但是,没有任何事情发生,没有任何图像显示出来。我做错了什么?

2 个答案:

答案 0 :(得分:0)

我还没有尝试过,但也许可以尝试将代码包装在这个块中:

[UIView animateWithDuration:6f animations:^{

    [UIImage animatedImageWithImages:animationFrames duration:6.0f]

} completion:^(BOOL finished) {
    //Callback after animation was completed
    [view removeFromSuperview];
}];

答案 1 :(得分:0)

下面的一些代码可以尝试。 请注意,您不需要重复imageNamed:系列,只需根据需要设置一个循环的持续时间。

myImageWalk = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 568)];
myImageWalk.image = [self loadingImage];
self.myImageWalk.animationRepeatCount = 0;

[window addSubview:myImageWalk];
[window bringSubviewToFront:myImageWalk];

//when you want the animation to start
[self animateImages];
//then later at some appropriate time:
[self stopAnimatingImages];

- (UIImage*)loadingImage {
    NSArray *animationFrames = [NSArray arrayWithObjects:
                            [UIImage imageNamed:@"original.png"],
                            [UIImage imageNamed:@"second.png"],
                            nil];
    return [UIImage animatedImageWithImages:animationFrames duration:6.0f];
}
- (void)animateImages {
    if (![self.myImageWalk isAnimating]) {
        [self.myImageWalk startAnimating];
    }
}
- (void)stopAnimatingImages {
    if ([self.myImageWalk isAnimating]) {
        [self.myImageWalk stopAnimating];
    }
}

注意:除非这是测试应用程序或类分配,否则应将此逻辑从AppDelegate移至ViewContoller,因为最佳做法是尽量减少didFinishLaunching期间完成的工作。