iPhone SDK:为什么下面的代码只对第一帧进行动画处理?

发布于 2024-07-24 06:57:23 字数 4926 浏览 1 评论 0原文

我一直在尝试使用 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 正常工作了,我的下一步是停止我的动画一遍又一遍地重复,所以我使我的 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)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;

}

I have been trying to animate using an NSTimer and I've set up my code like this:

- (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;


}

Does anyone know why this only animates the first frame? The problem's probably very simple to fix, i'm just too much of a noob in iPhone development to notice.
----------Edit----------

Okay, I have got my NSTimer to work correctly, and my next step was to stop my animation repeating over and over, so I invalidated my timer like this:

[animationTimer invalidate];

And now, when the timer is called again it doesn't work, so how would I validate it again? (re-instantiate it I think)

(BTW I Have set up another SO question for this)

My code as of now:

- (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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

能否归途做我良人 2024-07-31 06:57:23

你正在以艰难的方式去做这件事。

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

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

You're going about it the hard way.

  • Create a UIImageView and put it on the screen.
  • Load your images into an NSArray.
  • Set the UIImageView's animationImages property to the array.
  • You can adjust the animationDuration and animationRepeatCount properties as well.
  • When user presses the start button, call the startAnimating method of the image view.

You don't need the timer nor the manual frame advancing code.

送舟行 2024-07-31 06:57:23

如果没有看到您提供的更多代码,就没有什么好的方法可以知道。 如何存储“戈登”? 它是什么类型? 大概是某种类型的 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++];
    }
}

There's no good way to know without seeing more code from you. How do you store "gordon"? What type is it? presumably some type of UIImageView?

However, your real problem (if I may go to a higher level) is your entire structure. Try something more like this:

// Interface file

NSMutableArray *pigImages;
int pigImageIndex;

// Implementation file

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++];
    }
}
安静 2024-07-31 06:57:23

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

To restart your timer you can use -[NSRunLoop addTimer:forMode:] or just release and create a new timer each time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文