4

我正在使用以下代码显示时间...我的 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];
}
4

3 回答 3

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]; 
于 2012-04-22T06:29:41.240 回答
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.

于 2012-04-21T13:32:19.350 回答
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;

}
于 2012-04-21T13:04:06.207 回答