views:

55

answers:

4

Hi everyone,

I'm creating a small stop watch with UIImageViews for the digits.I have 2 views controllers.One is the main view controller and when you press the "Stop Watch" button it loads the 2nd view controller with stop watch.I have created a NSTimer like this....

timerMain = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(setDigitImages) userInfo:nil repeats:YES];

Inside setDigitImages method, I change the digits and when stop button is pressed, I stop the timer like this...

[timerMain invalidate];
[timerMain release];
timerMain = nil;

I started the timer and stopped it and went back to the main view controller.When I do this 5 to 6 times, the digit changing slows down. Can anyone help with this?....

Thanks....

+1  A: 

Are you reusing or recreating your second view and all the digit image views? If you are recreating them, did you properly release the previous ones? If not, that could cause memory issues to slow things down.

NSTimers won't make up for lost time between repeat calls. Therefore you shouldn't assume that exactly 0.01 seconds has elapsed between calls to your setDigitImages method. Instead you should explicitly check inside this method for the elapsed time since the start button was pressed (save that initial time and subtract) and use that for the current digits.

hotpaw2
yes...I'm relesing all the object in the dealloc and it also gets called....
deamonsarea
Can you help me with the code to get the elapsed time?.....
deamonsarea
A: 

Here is the setDigitImages method....

- (void)setDigitImages {

    iPoints++;

    if (iPoints==100) {
    iSeconds++;
    iPoints=0;
    [self changeColorOfNumbers];
    NSString *strImgSecond2 = [NSString stringWithFormat:@"%d.png",iSeconds];
    imgSeconds2.image = [UIImage imageNamed:strImgSecond2];
    imgPoints1.image = [UIImage imageNamed:@"0.png"];
    imgPoints2.image = [UIImage imageNamed:@"0.png"];
}

if (iPoints<10) {
    NSString *strImgName = [NSString stringWithFormat:@"0%d.png",iPoints];
    NSString *strPoint1 = [strImgName substringToIndex:1];
    strPoint1 = [strPoint1 stringByAppendingString:@".png"];
    imgPoints1.image = [UIImage imageNamed:strPoint1];
    NSString *strPoint2 = [strImgName substringFromIndex:1];
    imgPoints2.image = [UIImage imageNamed:strPoint2];

} else {
    NSString *strNameSplitter = [NSString stringWithFormat:@"%d",iPoints];
    NSString *strImgName = [strNameSplitter substringToIndex:1];
    strImgName = [strImgName stringByAppendingString:@".png"];
    imgPoints1.image = [UIImage imageNamed:strImgName];
    NSString *strImgPoint2 = [strNameSplitter substringFromIndex:1];
    strImgPoint2 = [strImgPoint2 stringByAppendingString:@".png"];
    imgPoints2.image = [UIImage imageNamed:strImgPoint2];
}

if (iSeconds>9) {
    NSString *strNameSplitter = [NSString stringWithFormat:@"%d",iSeconds];
    NSString *strImgSecond1 = [strNameSplitter substringToIndex:1];
    strImgSecond1 = [strImgSecond1 stringByAppendingString:@".png"];
    imgSeconds1.image = [UIImage imageNamed:strImgSecond1];
    NSString *strImgSecond2 = [strNameSplitter substringFromIndex:1];
    strImgSecond2 = [strImgSecond2 stringByAppendingString:@".png"];
    imgSeconds2.image = [UIImage imageNamed:strImgSecond2];
}
}

Thanks....

deamonsarea
+1  A: 

Your stopwatch won't be very accurate if you do this, since there's no guarantee that NSTimer will fire every single 1/100th of a second, or that its timing will be especially accurate.

You should restructure your app to use a genuine timer. The system clock is a good starting point. Use [[NSDate date] timeIntervalSinceReferenceDate] to read the current time at the moment the watch is started, and store this value. Then when it's time to redraw the stopwatch, read the time again and subtract the starting value, and display that number.

grahamparks
Thanks grahamparks....
deamonsarea
A: 

I found the bug.

Slowing the timer caused of multiple timers running and not stopping them....

But I think grahamparks suggestion would be the best.......

Thanks for the help.

deamonsarea