4

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];
}
Community
  • 1
  • 1
SriKanth
  • 459
  • 7
  • 27

3 Answers3

11

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]; 
Shaheen M Basheer
  • 1,010
  • 6
  • 11
  • Also relevant (and my biggest gottcha): each thread has its own RunLoop object. [[NSRunLoop currentRunLoop] will give you the current thread's runloop object; and depending on your code if you don't schedule your timer on the main thread your timer may never tick. – pretzels1337 Sep 21 '12 at 19:51
5

Timers are scheduled in run loops in certain run loop modes. They can only fire when the run loop is being run in one of those modes. The +scheduledTimerWithTimeInterval:... methods schedule the timer in the default mode. Tracking of events while a scroll bar is being manipulated uses NSEventTrackingRunLoopMode. You need to schedule the timer on the proper mode(s).

You might schedule it on NSRunLoopCommonModes which is a virtual set of modes. Run loops never run in such a mode, but they run in modes which are members of that set. The default mode and NSEventTrackingRunLoopMode are added to that set, as is NSModalPanelRunLoopMode.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
1
IBOutlet UILabel * result;
NSTimer * timer;                
int currentTime;


- (IBAction) start;
- (IBAction) pause;
- (void)populateLabelwithTime:(int)milliseconds;

.m file

- (void)viewDidLoad 
{
    currentTime = 270000000; // Since 75 hours = 270000000 milli seconds
    // ..... some codes....
}
- (IBAction) start
{
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
}

-(IBAction)pause
{
    [timer invalidate]; 
}

- (void)updateTimer:(NSTimer *)timer {
    currentTime -= 10 ;
    [self populateLabelwithTime:currentTime];
}
- (void)populateLabelwithTime:(int)milliseconds 
{
    int seconds = milliseconds/1000;
    int minutes = seconds / 60;
    int hours = minutes / 60;

    seconds -= minutes * 60;
    minutes -= hours * 60;

    NSString * result1 = [NSString stringWithFormat:@"%@%02dh:%02dm:%02ds:%02dms", (milliseconds<0?@"-":@""), hours, minutes, seconds,milliseconds%1000];
    result.text = result1;

}
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
  • Hai... I have tried this code... I have ScrollView in my View Controller, I am displaying the time in that view controller.. If Drag and hold the scroll view the time is not calculating (in the background also)... How can i..? – SriKanth Apr 21 '12 at 13:08