【问题标题】:NStimer in not working properly in iOSNStimer 在 iOS 中无法正常工作
【发布时间】:2012-04-21 12:57:20
【问题描述】:

我正在使用以下代码显示时间...我的 viewController 中有滚动视图...这里我显示的时间从 00:00.00 (mm:ss:SS) (minutes:seconds:milliseconds) 开始并瞄准增加毫秒,基于毫秒的秒,基于秒的分钟......但我想显示从 75:00.00 (mm:ss:SS) 开始的时间,我想将毫秒、秒和分钟减少到 00:00.00 ( mm:ss.SS)...我怎么能...?

我已经在下面的链接中问过这个问题。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];
}

【问题讨论】:

    标签: iphone objective-c ios5 xcode4.2 nstimer


    【解决方案1】:

    你必须注册定时器才能在 NSRunLoopCommonModes 中运行

    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10 
                                                      target:self 
                                                    selector:@selector(showActivity:) 
                                                    userInfo:nil 
                                                     repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:stopWatchTimer forMode:NSRunLoopCommonModes]; 
    

    【讨论】:

    • 也相关(也是我最大的问题):每个线程都有自己的 RunLoop 对象。 [[NSRunLoop currentRunLoop] 会给你当前线程的runloop对象;并且根据您的代码,如果您不在主线程上安排计时器,您的计时器可能永远不会滴答作响。
    【解决方案2】:

    在某些运行循环模式下,定时器被安排在运行循环中。它们只能在运行循环以其中一种模式运行时触发。 +scheduledTimerWithTimeInterval:... 方法以默认模式调度计时器。在操纵滚动条时跟踪事件使用NSEventTrackingRunLoopMode。您需要将计时器安排在正确的模式下。

    您可以在NSRunLoopCommonModes 上安排它,这是一组虚拟模式。运行循环永远不会在这种模式下运行,但它们会在属于该集合成员的模式下运行。默认模式和NSEventTrackingRunLoopMode 被添加到该集合中,NSModalPanelRunLoopMode 也是如此。

    【讨论】:

      【解决方案3】:
      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;
      
      }
      

      【讨论】:

      • Hai...我已经尝试过这段代码...我的视图控制器中有 ScrollView,我正在该视图控制器中显示时间。如果拖动并按住滚动视图时间不是计算(也在后台)......我怎么能......?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2014-09-08
      • 1970-01-01
      相关资源
      最近更新 更多