TimePicker以毫秒为单位倒计时

问题描述:

我一直在尝试2天,搜索高和低,无法获得毫秒的工作。小时,分钟&秒工作正常,但毫秒不会。TimePicker以毫秒为单位倒计时

我已经做了一个lapCounter,它向上计数并且毫秒没有问题。

下面是lapCounter,其计数的工作代码和毫秒工作:

int hours = (UInt8)(elapsedTime /(60*60)); 
    int mins = (UInt8)(elapsedTime/60.0); 
    elapsedTime -= (mins * 60); 
    int secs = (UInt8)(elapsedTime); 
    elapsedTime -= (secs); 
    int mms = (UInt8)(elapsedTime * 100); 

但我不能让TimePicker倒计时,工作。

这就是我对TimePicker倒计时:

int afterRemainder; 
int remainder; 
NSTimeInterval countDownTime; 
NSTimer *countDownTimer; 
bool startCountDown; 

- (IBAction)startCountDownButton:(id)sender { 
    if (startCountDown == false) { 
     countDownTime = (NSTimeInterval)_datePicker.countDownDuration; 
     remainder = countDownTime; 
     afterRemainder = countDownTime - remainder%60; 

     countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCountDown) userInfo:nil repeats:YES]; 

     startCountDown = true; 
} 

-(void)updateCountDown { 
    afterRemainder --; 

    int hours = (int)(afterRemainder/(60 * 60)); 
    int mins = (int)(afterRemainder/60) - (60 * hours); 
    int secs = (int)(afterRemainder - (60 * mins) - (60 * hours * 60)); 
    int mms = (int)(afterRemainder - (3600 * secs) - (mins * 60)); 
    self.displayCountDown.text = [[NSString alloc] initWithFormat:@"%02d", hours]; 
    self.displayCountDownMins.text = [[NSString alloc] initWithFormat:@": %02d", mins]; 
    self.displayCountDownSecs.text = [[NSString alloc] initWithFormat:@"%02d", secs]; 
    self.displayCountDownMMs.text = [[NSString alloc] initWithFormat:@":%2d", mms]; 
} 
+0

定义 “不工作”。究竟是什么问题?在你的问题中给出清晰的例子(不在评论中)。 – rmaddy

+0

@rmaddy嗨感谢张贴。毫秒不会以毫秒为单位倒数。一些疯狂的数字出现了,我尝试了一切。 –

+0

@rmaddy这是数毫秒失败的数学。 –

试试这个:

@interface ViewController() 

@property (nonatomic, weak) IBOutlet UILabel *hours; 
@property (nonatomic, weak) IBOutlet UILabel *minutes; 
@property (nonatomic, weak) IBOutlet UILabel *seconds; 
@property (nonatomic, weak) IBOutlet UILabel *millisecs; 

@property (nonatomic, readwrite) NSTimeInterval counter; 
@property (nonatomic, strong) NSTimer *timer; 

@property (nonatomic, readwrite) int hoursInt; 
@property (nonatomic, readwrite) int minutesInt; 
@property (nonatomic, readwrite) int secondsInt; 
@property (nonatomic, readwrite) int millisecsInt; 

@end 

@implementation ViewController 

#define MS_COUNT_STEP 20.0 // Approx 50 fps 

- (IBAction)countUpPressed:(id)sender { 
    [self.timer invalidate]; 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:MS_COUNT_STEP/1000.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES]; 
} 

- (IBAction)countDownPressed:(id)sender { 
    [self.timer invalidate]; 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:MS_COUNT_STEP/1000.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES]; 
} 

- (void)countUp { 
    self.counter += (MS_COUNT_STEP/1000.0); 
} 

- (void)countDown { 
    self.counter -= (MS_COUNT_STEP/1000.0); 
} 

- (void)setCounter:(NSTimeInterval)counter { 
    _counter = counter; 
    [self updateUI]; 
} 

- (void)updateUI { 
    NSTimeInterval counter = _counter; 

    self.hoursInt = counter/3600; 
    counter -= ((int)self.hoursInt * 3600); 

    self.minutesInt = counter/60; 
    counter -= ((int)self.minutesInt * 60); 

    self.secondsInt = (int)counter; 
    counter -= self.secondsInt; 

    self.millisecsInt = counter * 1000; 
} 

- (void)setHoursInt:(int)value { 
    if (value != _hoursInt) { 
     _hoursInt = value; 
     self.hours.text = [NSString stringWithFormat:@"%i", value]; 
    } 
} 

- (void)setMinutesInt:(int)value { 
    if (value != _minutesInt) { 
     _minutesInt = value; 
     self.minutes.text = [NSString stringWithFormat:@"%i", value]; 
    } 
} 

- (void)setSecondsInt:(int)value { 
    if (value != _secondsInt) { 
     _secondsInt = value; 
     self.seconds.text = [NSString stringWithFormat:@"%i", value]; 
    } 
} 

- (void)setMillisecsInt:(int)value { 
    if (value != _millisecsInt) { 
     _millisecsInt = value; 
     self.millisecs.text = [NSString stringWithFormat:@"%i", value]; 
    } 
} 

@end 
+0

嗨,感谢代码,它确实会产生毫秒,但我无法将它连接到datePicker。我在最后添加了代码,关于我如何尝试连接它。 –

+0

这就是我试图将datePicker连接到你的“计数器”的方法。 { self.countDownTime =(NSTimeInterval)_datePicker.countDownDuration; remaining = self.countDownTime; _counter = self.countDownTime - 余数; self.timer = [NSTimer scheduledTimerWithTimeInterval:MS_COUNT_STEP/1000.0 target:self selector:@selector(countDown)userInfo:nil repeatats:YES]; } –

+0

难道你只是使用:“self.counter = self.dataPicker.countDownDuration”? – norders

使用CADisplayLink,相反的NSTimer。

CADisplayLink对象是一个计时器对象,它允许应用程序将其绘图与显示刷新率同步。

使用[NSTimer scheduledTimerWithTimeInterval:1 ...],系统无法以该速度绘制标签。