[英]iPhone SDK: After invalidating a Timer, how can I use it again?
我已經使用 NSTimer 在 Xcode 中設置了一個 animation,它一遍又一遍地重復,所以我使用了這個命令:
else if(gordon.image == pigImage11)
[animationTimer invalidate];
因此,當 gordon(一個 UIImageView)圖像設置為 pigImage11 時,計時器失效,這達到了停止 animation 不斷重復的預期效果,但停止了再次使用計時器,那么我如何讓計時器再次可用,但是有它在那個框架上使自己失效? 為了進一步說明,這是我的全部代碼:
- (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];
}
您可以像這樣重用計時器:
- (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;
}
}
不使計時器無效而只設置一個“不動畫”標志怎么樣?
[[NSRunLoop currentRunLoop] addTimer:animationTimer
forMode:NSDefaultRunLoopMode];
但是你當然不能在無效后釋放定時器。
為什么不使用 animation 中內置的 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 中尋找什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.