活動(dòng)倒計(jì)時(shí)關(guān)于時(shí)間的處理

<code>

  • (void)timeWithStr:(NSString )str{
    <code>
    NSDate
    dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970]1000;
    NSInteger b = (NSInteger)a;//當(dāng)前系統(tǒng)時(shí)間戳
    NSInteger x = [str integerValue];
    NSInteger d = (x -b);
    CGFloat tep = 24
    60601000;//tep 表示一天
    if (x > tep) { //顯示天數(shù)
    NSInteger day = floor((d/tep));//向下取整
    [self.surplusButton setTitle:[NSString stringWithFormat:@"剩余:%ld天",day] forState:UIControlStateNormal];
    }else{//進(jìn)入倒計(jì)時(shí)
    //進(jìn)入倒計(jì)時(shí)后將(毫秒數(shù)差)轉(zhuǎn)成小時(shí)取整,分取整,秒取整
    self.timeSec = d/1000;
    [self startTimer];
    }
    </code>
    }
  • (void)startTimer

{
<code>
if (timer) {
[timer invalidate];
}
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshLessTime) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
</code>
}

  • (void)refreshLessTime{
    <code>
    [self.surplusButton setTitle:[NSString stringWithFormat:@"剩余:%02ld:%02ld:%02ld",self.timeSec/3600,self.timeSec%3600/60,self.timeSec%60] forState:UIControlStateNormal];
    </code>
    self.timeSec--;
    if (self.timeSec==0) {
    [self.surplusButton setTitle:@"已結(jié)束" forState:UIControlStateNormal];
    self.surplusButton.backgroundColor = [UIColor lightGrayColor];
    [timer invalidate];
    timer = nil;
    }
    }
    </code>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容