我正在使用以下代码显示时间...我的 viewController 中有滚动视图...这里我显示的时间从 00:00.00 (mm:ss:SS) (minutes:seconds:milliseconds) 开始,目标是增加毫秒,基于毫秒的秒,基于秒的分钟...但是我想显示从 75:00.00 (mm:ss:SS) 开始的时间,并且我想将毫秒、秒和分钟递减到 00:00.00 (mm:ss .SS)...我怎么能...?
我已经在下面的链接中问过这个问题.. NSTimer 将时间减少秒/毫秒
我遵循该代码,当我拖动并按住滚动视图而不释放鼠标单击时,时间没有计算(也在后台)...帮助我更改以下代码...
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];
}