【问题标题】:TimePicker Count Down in MillisecondsTimePicker 以毫秒为单位倒计时
【发布时间】:2016-09-15 02:02:57
【问题描述】:

我已经尝试了 2 天,搜索高低,无法让毫秒工作。小时、分钟和秒可以正常工作,但毫秒不会。

我制作了一个 lapCounter,它可以向上计数并且毫秒数没有问题。

这是 lapCounter 的工作代码,它计数 UP 和毫秒工作:

    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 Count DOWN,工作。

这就是我为 TimePicker Count DOWN 准备的内容:

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];
}

【问题讨论】:

  • 定义“不起作用”。究竟是什么问题?在你的问题中给出明确的例子(不是在 cmets 中)。
  • @rmaddy 嗨,感谢您的发帖。毫秒不以毫秒为单位倒计时。出现了一些疯狂的数字,我已经尝试了所有方法。
  • @rmaddy 失败的毫秒数。
  • 为什么你的小时、分钟、秒和毫米的上下计算有如此大的不同?它们应该是相同的。
  • 我试过了。这就是我不明白的。但是小时、秒和分钟都可以正常工作。

标签: ios objective-c countdown timepicker milliseconds


【解决方案1】:

试试这个:

@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

【讨论】:

  • 您好,感谢您的代码,它确实会产生毫秒,但我无法将它连接到 datePicker。我在最后添加了代码,说明我是如何尝试连接它的。
  • 这就是我尝试将 datePicker 连接到您的“计数器”的方式。 { self.countDownTime = (NSTimeInterval)_datePicker.countDownDuration;余数 = self.countDownTime; _counter = self.countDownTime - 余数; self.timer = [NSTimer scheduledTimerWithTimeInterval:MS_COUNT_STEP / 1000.0 目标:self 选择器:@selector(countDown) userInfo:nil repeats:YES]; }
  • 你不就用:“self.counter = self.dataPicker.countDownDuration”吗?
  • 好的,我按照建议设置了它,它可以工作!但它开始于实际时间之上。因此,如果 datePicker 有 10:00,它将从 10:00 开始,但顶部有 6 秒。所以它必须经过 6 秒才能到达它应该开始的地方,也就是 9:59:59。有什么想法吗?
  • 啊。您需要忽略秒组件,因为 UIDatePicker 只有 1 分钟的结果。所以:“self.counter = 60 * (int)(self.datePicker.countDownDuration / 60);”
【解决方案2】:

使用 CADisplayLink,而不是 NSTimer。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多