我使用NSTimer在Xcode中设置了一个动画,并且它一遍又一遍地重复,所以我使用了这个命令:
else if(gordon.image == pigImage11)
[animationTimer invalidate];
因此,当gordon(一个UIImageView)图像设置为pigImage11时,计时器无效,这给出了停止动画不断重复所需的效果,但是停止了再次使用的计时器,那么如何让计时器再次使用,但是它会在那个框架上自动失效吗? 为了进一步说明,这是我的整个代码:
- (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;
}
- (void)stopTimer
{
[animationTimer invalidate];
[animationTimer release];
}
答案 0 :(得分:4)
您可以重复使用这样的计时器:
- (void) startTimer {
if ( myTimer == nil ) {
myTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0f
target: self
selector: @selector(myTimerAction:)
userInfo: nil
repeats: NO];
}
}
- (void) stopTimer {
if ( myTimer || [myTimer isValid])
{
[myTimer invalidate];
myTimer = nil;
}
}
答案 1 :(得分:0)
如何不使计时器失效而只是设置“不要动画”标志呢?
答案 2 :(得分:0)
[[NSRunLoop currentRunLoop] addTimer:animationTimer
forMode:NSDefaultRunLoopMode];
但是,当然,你必须在无效后释放计时器。
答案 3 :(得分:0)
为什么不使用UIImageView的内置动画:
UIImageView* gordonView = [[UIImage alloc]init];
gordonview.animationImages = [NSArray arrayWithObjects: pigImage1, pigimage2, etc., nil];
gordonView.animationDuration = 0.5f; // about 30fps with your 13 images.
//Set gordon's frame here
[gordonView startAnimating];
披露,我把它键入SO,而不是Xcode我确定它不会编译,但至少你知道在SDK中寻找什么。