我正在使用以下代码显示时间...我在viewController中有滚动视图...这里我显示的时间从00:00.00开始(mm:ss:SS)(分钟:秒:毫秒)和目标基于毫秒增加毫秒,秒,基于秒的分钟...但我想显示从75:00.00(mm:ss:SS)开始的时间,我想将毫秒,秒和分钟递减到00:00.00( mm:ss.SS)......我怎么能......?
我已在SO中通过以下链接询问了这一点。NSTimer Decrease the time by seconds/milliseconds
我遵循该代码的时间不计算(在后台也是)当我拖动并按住滚动视图而不释放鼠标点击...帮我修改下面的代码......
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];
}
答案 0 :(得分:11)
您必须注册计时器才能在NSRunLoopCommonModes中运行
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10
target:self
selector:@selector(showActivity:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:stopWatchTimer forMode:NSRunLoopCommonModes];
答案 1 :(得分:5)
在某些运行循环模式下,计时器在运行循环中进行调度。它们只能在以其中一种模式运行运行循环时触发。 +scheduledTimerWithTimeInterval:...
方法将计时器安排在默认模式下。在操纵滚动条时跟踪事件使用NSEventTrackingRunLoopMode
。您需要在正确的模式下安排计时器。
您可以在NSRunLoopCommonModes
上安排它,这是一组虚拟模式。运行循环永远不会在这种模式下运行,但它们以该集合的成员模式运行。默认模式和NSEventTrackingRunLoopMode
会添加到该集合中,NSModalPanelRunLoopMode
也是如此。
答案 2 :(得分:1)
IBOutlet UILabel * result;
NSTimer * timer;
int currentTime;
- (IBAction) start;
- (IBAction) pause;
- (void)populateLabelwithTime:(int)milliseconds;
.m文件
- (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;
}