Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NStimer in not working properly in iOS

I am displaying time using the following code... I have scroll view in my viewController... Here i am displaying time starting with 00:00.00 (mm:ss:SS) (minutes:seconds:milliseconds) and aim incrementing the milliseconds , seconds based on milliseconds, minutes based on seconds... But i want to display time starting from 75:00.00 (mm:ss:SS) and i want to decrement the milliseconds, seconds and minutes to 00:00.00 (mm:ss.SS)... How can I..?

I have already asked this in SO in the following link.. NSTimer Decrease the time by seconds/milliseconds

I i follow that code the time is not calculating (in the background also) when i drag and hold the scroll view with out releasing the mouseClick... Help me with the changes in below code...

enter code here
@interface ViewController : UIViewController
{
    IBOutlet UILabel *time;
    NSTimer *stopWatchTimer;
    NSDate *startDate;
    NSTimeInterval secondsAlreadyRun;
}

- (void)reset:(id)sender;
- (void)onStartPressed:(id)sender; 
- (void)onStopPressed:(id)sender;


enter code here
-(void)showActivity:(NSTimer *)tim 
{
    NSDate *currentDate = [NSDate date];

    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    // Add the saved interval
    timeInterval += secondsAlreadyRun;
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    static NSDateFormatter * dateFormatter = nil;
    if( !dateFormatter ){
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"mm:ss.SS"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    }
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    time.text = timeString;

    //    [dateFormatter release];
}

- (void)onStartPressed:(id)sender 
{
    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10 
                                                      target:self 
                                                    selector:@selector(showActivity:) 
                                                    userInfo:nil 
                                                     repeats:YES];
    // Save the new start date every time
    startDate = [[NSDate alloc] init]; // equivalent to [[NSDate date] retain];
    [stopWatchTimer fire];
}

- (void)onStopPressed:(id)sender
{
    // _Increment_ secondsAlreadyRun to allow for multiple pauses and restarts
    secondsAlreadyRun += [[NSDate date] timeIntervalSinceDate:startDate];
    [stopWatchTimer invalidate];
    stopWatchTimer = nil;

    //    [startDate release];
    //    [self showActivity:stopWatchTimer];
}
like image 370
SriKanth Avatar asked Dec 04 '22 04:12

SriKanth


1 Answers

You have to register the timer to run in NSRunLoopCommonModes

stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10 
                                                  target:self 
                                                selector:@selector(showActivity:) 
                                                userInfo:nil 
                                                 repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:stopWatchTimer forMode:NSRunLoopCommonModes]; 
like image 184
Shaheen M Basheer Avatar answered Dec 12 '22 02:12

Shaheen M Basheer