2

startstop、 の3 つのボタンをセットアップしpauseました。NSTimerに制御を与えて計算します.start stopボタンは正常に機能し、開始時間と停止時間を提供しますが、pauseボタンは正確な時間を提供しません.実際には時間を一時停止します..しかし、もう一度開始すると、一時停止した時間と表示が追加されます スタートの 5 秒で一時停止し、5 秒待ってからスタートを押すとします... 5 と表示されるはずですが、10 と表示されます..

-(void)start:(NSTimer *)timer
{
  if(_timer==nil)
  {
    startDate =[NSDate date];

    _timer=[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
  }

  if(_timer!=nil)
  { 
    float pauseTime = -1*[pauseStart timeIntervalSinceNow];

    [_timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
  }

}

-(void)timer:(NSTimer *)timer
{
  NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];

  NSInteger seconds = secondsSinceStart % 60;
  NSInteger minutes = (secondsSinceStart / 60) % 60;
  NSInteger hours = secondsSinceStart / (60 * 60);
  NSString *result = nil;
  if (hours > 0) 
  {
    result = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
  }
  else 
  {
    result = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];        
  }

  label.text=result;

  NSLog(@"time interval -> %@",result);
}

-(void)stop
{
  if(_timer!=nil)
  {
    endDate = [NSDate date];
 NSLog(@"endate%@",endDate);

     NSTimeInterval interval = [endDate timeIntervalSinceDate:startDate];
NSLog(@"total time %f",interval);
    [_timer invalidate];
    _timer = nil; 
  startDate=nil;
  }
}

-(void)pause:(NSTimer *)timer
{
  pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];

  previousFireDate = [_timer fireDate];

  [_timer setFireDate:[NSDate distantFuture]];
}
4

2 に答える 2

1

NSTimerは一時停止できないため、決して機能しません。開始し、無効にするときにのみ停止します。有効な状態の場合は実行中であることを意味し、状態が無効な場合はタイマーが停止していることを意味します。ありがとう

于 2012-12-31T13:04:21.430 に答える
1

このアプリケーションは mac os で作成しました。ロジックを理解し、小さな変更を加えてこれをコピーすることもできると思います... UILabel.

.h で

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

- (IBAction)start:(id)sender;
- (IBAction)pause:(id)sender;
- (IBAction)stop:(id)sender;
@property (strong) IBOutlet NSTextField *label;

@property (strong)NSDate *startDate;
@property (strong)NSTimer *timer;

@property (assign)BOOL isRunning;
@property (assign)BOOL isPaused;

@property(assign)NSInteger secondsSinceStart;

@property(assign)NSInteger secondsPaused;
@end

.m

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    self.label.stringValue=@"00:00:00";
    self.isRunning=NO;
    self.isPaused=NO;
    self.secondsPaused=0;
}

-(void)timerDisplay{

    if (self.isPaused) {
        self.secondsPaused++;
        return;
    }

    self.secondsSinceStart+=1;

    NSInteger seconds = self.secondsSinceStart % 60;
    NSInteger minutes = (self.secondsSinceStart / 60) % 60;
    NSInteger hours = self.secondsSinceStart / (60 * 60);
    NSString *result = nil;


    if (self.isRunning && !self.isPaused) {
        result = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hours, minutes, seconds];
        self.label.stringValue=result;
    }
}


- (IBAction)start:(id)sender {
    self.isRunning=!self.isRunning;
    self.isPaused=NO;
    self.secondsSinceStart=0;
    self.label.stringValue=@"00:00:00";


    self.startDate =[NSDate date];
    if (!self.timer) {
        self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerDisplay) userInfo:nil repeats:YES];
    }
}

- (IBAction)pause:(id)sender {
    self.isPaused=!self.isPaused;
    NSLog(@"pause : %d",self.isPaused);
}

- (IBAction)stop:(id)sender {
    self.isRunning=NO;
    NSLog(@"start : %@",self.startDate);
    NSLog(@"end : %@",[NSDate date]);
    NSLog(@"paused : %ld",self.secondsPaused);

    NSInteger totalTime=self.secondsSinceStart+self.secondsPaused;

    NSInteger seconds = totalTime % 60;
    NSInteger minutes = (totalTime / 60) % 60;
    NSInteger hours = totalTime / (60 * 60);
    NSString *result = result = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hours, minutes, seconds];
    NSLog(@"Total : %@",result);

}
@end
于 2012-12-31T14:40:32.737 に答える