2016-09-15 12 views
0

私は高低を検索して2日間試行しており、ミリ秒を稼働させることはできません。時間は、分の&秒は正常に動作しますが、ミリ秒は動作しません。TimePickerミリ秒でカウントダウン

私はlapCounterを作成しました。これはUPをカウントし、ミリ秒で問題はありません。ここで

はカウントアップ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は、DOWN仕事を数えることはできません。

これはTimePickerのために私が持っているものである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]; 
} 
+0

"動作しない" を定義します。何が問題なのですか?あなたの質問に明確な例を挙げてください(コメントではありません)。 – rmaddy

+0

@rmaddyこんにちは、投稿していただきありがとうございます。ミリ秒はミリ秒単位でカウントダウンしません。いくつかの狂った数字が出てきて、私はすべてを試しました。 –

+0

@rmaddy失敗するのはミリ秒単位の数学です。 –

答えて

0

はこれを試してみてください:

@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; remainder = self.countDownTime; _counter = self.countDownTime - 剰余; self.timer = [NSTimer scheduledTimerWithTimeInterval:MS_COUNT_STEP/1000.0ターゲット:セルフセレクタ:@セレクタ(countDown)userInfo:nil repeat:YES]; } –

+0

「self.counter = self.dataPicker.countDownDuration」を使用しないでください。 – norders

0

使用CADisplayLink、代わりにNSTimer。

CADisplayLinkオブジェクトは、アプリケーションがその図面をディスプレイのリフレッシュレートに同期させるためのタイマーオブジェクトです。

[NSTimer scheduledTimerWithTimeInterval:1 ...]を指定すると、システムはその速度でラベルを描画できません。