2010-06-19 80 views
0

這裏是我的每一秒後調用manageAnimation代碼應用程序崩潰因嵌套的NSTimer和視圖切換

-(void)manageAnimation:(Properties *)prop 
{ 
    //if(bounceTimer.isValid) 
    // [bounceTimer invalidate]; 


    bounceTimer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(animate) userInfo:nil repeats:YES]; 

} 
-(void)animate{ 
    static float t = 0; 
    float d = .5; 
    static float fValue = 0; 
    fValue = [self easeOutBounce:t andB:0 andC:34 andD:d]; 

    [self setNeedsDisplay]; 

    t += .5/10; 
    if(t > d){ 
     [bounceTimer invalidate]; 
     t = 0; 
     fValue = 0; 
    } 
} 

。當我點擊屏幕上的信息按鈕時,它會翻轉到設置屏幕,當我點擊完成按鈕在設置屏幕上返回到主屏幕應用程序崩潰。如果我註釋掉代碼

//bounceTimer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(animate) userInfo:nil repeats:YES]; 

然後應用程序工作正常。我不知道發生了什麼,我也發現當我切換到設置視圖時,這些定時器停止。

回答

1

你爲什麼每秒都會打電話給manageAnimation?如果你這樣做,那麼你每次都創建一個新的定時器,並且仍然運行前一個定時器。

由於定時器重複,您可以讓它運行,而不是每次調用manageAnimation時創建一個新定時器。

此外,您還需要保留您創建的計時器。我假設bounceTimer是伊娃,所以你需要這在您的.h文件中:

@property (nonatomic, retain) NSTimer *bounceTimer; 

然後manageAnimation需要使用的setter:

-(void)manageAnimation:(Properties *)prop 
{ 
    if (self.bounceTimer == nil) { 
     self.bounceTimer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(animate) userInfo:nil repeats:YES]; 
    } 
} 

而在有生命的,你將無效定時器,設置伊娃爲零釋放它:

[bounceTimer invalidate]; 
self.bounceTimer = nil; 
+0

是的你是對的我需要self.bounceTimer =零;再開始之前。謝謝。整天浪費在這... – coure2011 2010-06-19 19:13:36