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];
}
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With