iPhone SDK:为什么下面的代码只对第一帧进行动画处理?
我一直在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你正在以艰难的方式去做这件事。
NSArray
中。animationImages
属性设置为数组。animationDuration
和animationRepeatCount
属性。您不需要计时器,也不需要手动帧推进代码。
You're going about it the hard way.
UIImageView
and put it on the screen.NSArray
.animationImages
property to the array.animationDuration
andanimationRepeatCount
properties as well.startAnimating
method of the image view.You don't need the timer nor the manual frame advancing code.
如果没有看到您提供的更多代码,就没有什么好的方法可以知道。 如何存储“戈登”? 它是什么类型? 大概是某种类型的 UIImageView?
然而,你真正的问题(如果我可以更进一步的话)是你的整个结构。 尝试更多类似这样的内容:
// 接口文件
// 实现文件
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
// Implementation file
要重新启动计时器,您可以使用 -[NSRunLoop addTimer:forMode:] 或每次释放并创建一个新计时器。
To restart your timer you can use
-[NSRunLoop addTimer:forMode:]
or just release and create a new timer each time.