gpt4 book ai didi

ios - iPhone SDK : How come the following code only animates first frame?

转载 作者:行者123 更新时间:2023-11-28 20:48:05 25 4
gpt4 key购买 nike

我一直在尝试使用 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];

}- (无效)打勾{ [ self 动画 pig ];}- (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;

最佳答案

你正在努力解决它。

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

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

关于ios - iPhone SDK : How come the following code only animates first frame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/995658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com