开发者

Application crashing due to Nested NSTimer and view switch

here is the code

-(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;
    }
}

I am calling manageAnimation after each second. When i click info button on screen it flips to settings screen, when i click done button on settings screen to go back to main screen the application crashes. If i comment out the code

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

Then application works ok. I am not sure whats going on, also i found that when i switched to settings view these timers are stopped.


Why would you call manageAnimation every second? If you do that then you create a new timer every time and still have the previous timer running.

Since the timer repeats you can just let it run instead of creating a new one every time you call manageAnimation.

Also you need to retain the timer you create. I'm assuming that bounceTimer is an ivar, so you need this in your .h file:

@property (nonatomic, retain) NSTimer *bounceTimer;

And then manageAnimation needs to use the setter:

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

And in animate where you invalidate the timer, set the ivar to nil to release it:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜