iPhone SDK:为什么下面的代码只为第一帧动画?

时间:2009-06-15 11:45:43

标签: ios objective-c animation nstimer

我一直在尝试使用NSTimer制作动画,我设置了这样的代码:

- (IBAction)startClick:(id)sender{

animationTimer = [NSTimer scheduledTimerWithTimeInterval:(.1/25.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];

}

- (void)tick{
    [self animatePig];
}

- (void)animatePig{

    UIImage *pigImage1 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0004.png" ofType:nil] ];
    UIImage *pigImage2 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0005.png" ofType:nil] ];
    UIImage *pigImage3 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0006.png" ofType:nil] ];
    UIImage *pigImage4 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0007.png" ofType:nil] ];
    UIImage *pigImage5 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0008.png" ofType:nil] ];
    UIImage *pigImage6 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0009.png" ofType:nil] ];
    UIImage *pigImage7 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0010.png" ofType:nil] ];
    UIImage *pigImage8 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0011.png" ofType:nil] ];
    UIImage *pigImage9 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0012.png" ofType:nil] ];
    UIImage *pigImage10 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0013.png" ofType:nil] ];
    UIImage *pigImage11 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0014.png" ofType:nil] ];
    UIImage *pigImage12 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0015.png" ofType:nil] ];

    if(gordon.image == pigImage1)
        gordon.image = pigImage2;
    else if(gordon.image == pigImage2)
        gordon.image = pigImage3;
    else if(gordon.image == pigImage3)
        gordon.image = pigImage4;
    else if(gordon.image == pigImage4)
        gordon.image = pigImage5;
    else if(gordon.image == pigImage6)
        gordon.image = pigImage7;
    else if(gordon.image == pigImage7)
        gordon.image = pigImage8;
    else if(gordon.image == pigImage8)
        gordon.image = pigImage9;
    else if(gordon.image == pigImage9)
        gordon.image = pigImage10;
    else if(gordon.image == pigImage10)
        gordon.image = pigImage11;
    else if(gordon.image == pigImage11)
        gordon.image = pigImage12;
    else
        gordon.image = pigImage12;


}

有谁知道为什么这只会激活第一帧?这个问题可能很容易解决,我只是在iPhone开发中注意到了太多的菜鸟。 ---------- ----------编辑

好的,我已经让我的NSTimer正常工作,我的下一步就是一遍又一遍地停止我的动画重复,所以我这样使我的计时器无效:

[animationTimer invalidate];

现在,当再次调用计时器时,它不起作用,那么我将如何再次验证它? (我认为重新实例化)

(顺便说一下,我为此设置了另一个SO问题)

截至目前我的代码:

- (IBAction)startClick:(id)sender{

animationTimer = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:@selector(tick) userInfo:nil repeats:YES];

} - (void)tick {     [self animatePig]; } - (void)animatePig {

UIImage *pigImage1=[UIImage imageNamed:@"gordonapple0004.png"];
UIImage *pigImage2=[UIImage imageNamed:@"gordonapple0005.png"];
UIImage *pigImage3=[UIImage imageNamed:@"gordonapple0006.png"];
UIImage *pigImage4=[UIImage imageNamed:@"gordonapple0007.png"];
UIImage *pigImage5=[UIImage imageNamed:@"gordonapple0008.png"];
UIImage *pigImage6=[UIImage imageNamed:@"gordonapple0009.png"];
UIImage *pigImage7=[UIImage imageNamed:@"gordonapple0010.png"];
UIImage *pigImage8=[UIImage imageNamed:@"gordonapple0011.png"];
UIImage *pigImage9=[UIImage imageNamed:@"gordonapple0012.png"];
UIImage *pigImage10=[UIImage imageNamed:@"gordonapple0013.png"];
UIImage *pigImage11=[UIImage imageNamed:@"gordonapple0014.png"];
UIImage *pigImage12=[UIImage imageNamed:@"gordonapple0015.png"];
UIImage *pigImage13=[UIImage imageNamed:@"gordonapple0016.png"];


if(gordon.image == pigImage1)
    gordon.image = pigImage2;
else if(gordon.image == pigImage2)
    gordon.image = pigImage3;
else if(gordon.image == pigImage3)
    gordon.image = pigImage4;
else if(gordon.image == pigImage4)
    gordon.image = pigImage5;
else if(gordon.image == pigImage5)
    gordon.image = pigImage6;
else if(gordon.image == pigImage6)
    gordon.image = pigImage7;
else if(gordon.image == pigImage7)
    gordon.image = pigImage8;
else if(gordon.image == pigImage8)
    gordon.image = pigImage9;
else if(gordon.image == pigImage9)
    gordon.image = pigImage10;
else if(gordon.image == pigImage10)
    gordon.image = pigImage11;
else if(gordon.image == pigImage11)
    [animationTimer invalidate];

else
    gordon.image = pigImage1;

}

3 个答案:

答案 0 :(得分:2)

你正在努力解决这个问题。

  • 创建UIImageView并将其放在屏幕上。
  • 将图片加载到NSArray
  • 将UIImageView的animationImages属性设置为数组。
  • 您也可以调整animationDurationanimationRepeatCount属性。
  • 当用户按下开始按钮时,请调用图像视图的startAnimating方法。

您不需要计时器,也不需要手动帧推进代码。

答案 1 :(得分:1)

如果没有看到更多来自您的代码,就没有好的方法可以了解。你如何存储“gordon”?它是什么类型的?大概是某种类型的UIImageView?

然而,你真正的问题(如果我可能会更上一层楼)是你的整个结构。尝试更像这样的东西:

//接口文件

NSMutableArray *pigImages;
int pigImageIndex;

//实施文件

const int firstImageIndex = 4;
const int lastImageIndex = 15;
const int imageCount = (lastImageIndex + 1 - firstImageIndex);

- (void) initPigImages
{
    pigImages = [[NSMutableArray alloc] initWithCapacity: imageCount];
    for (int i = firstImageIndex; i <= lastImageIndex; ++i)
    {
        [pigImages addObject: [self makePigImageWithIndex: i]];
    }
    pigImageIndex = 0;
}

- (UIImage*) makePigImageWithIndex: (int) imageIndex
{
    NSString *imageName = [NSString stringWithFormat: @"gordonapple%4d.png", imageIndex]; // Formatting may be a little off.
    return [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: imageName ofType: nil]]];
}

- (void )animatePig
{
    if (pigImageIndex < imageCount)
    {
        gordon.image = pigImages[pigImageIndex++];
    }
}

答案 2 :(得分:0)

要重新启动计时器,您可以使用-[NSRunLoop addTimer:forMode:]或只是每次都释放并创建一个新的计时器。