項(xiàng)目中提交訂單之后津滞,會(huì)有付款時(shí)間倒計(jì)時(shí)提醒铝侵,不過相關(guān)的接口中只提供了訂單提交時(shí)間戳,沒有訂單付款超時(shí)時(shí)間戳触徐,后臺(tái)規(guī)定咪鲜,訂單付款周期為24小時(shí),根據(jù)這個(gè)需求來進(jìn)行時(shí)間倒計(jì)時(shí)撞鹉。
調(diào)用以下的方法得出當(dāng)前時(shí)間與超時(shí)時(shí)間的間距:
//time:周期時(shí)間 ? 單位為秒數(shù) ? ? ?timeStamp:訂單提交時(shí)間戳+ (NSTimeInterval)timerInterval:(NSTimeInterval)interval timeStamp:(NSString *)timeStamp{
//訂單下單時(shí)間
NSDate *beginDate = [NSDate dateWithTimeIntervalSince1970:[timeStamp intValue]];
//訂單超時(shí)時(shí)間
NSDate *endDate = [NSDate dateWithTimeInterval:interval sinceDate:beginDate];
//當(dāng)前時(shí)間
NSDate *nowDate = [NSDate date];
NSTimeInterval nowInterval = [nowDate timeIntervalSince1970];
NSTimeInterval endInterval = [endDate timeIntervalSince1970];
//得出當(dāng)前時(shí)間與超時(shí)時(shí)間的間距
NSTimeInterval value = endInterval - nowInterval;
return value;
}
再進(jìn)行時(shí)間的判斷疟丙,當(dāng)value > 0 時(shí),計(jì)時(shí)器啟動(dòng)倒計(jì)時(shí)鸟雏,小于等于0時(shí)享郊,訂單已超時(shí),計(jì)時(shí)器停止孝鹊。
當(dāng)?value > 0時(shí)炊琉,計(jì)時(shí)器啟動(dòng),調(diào)用以下方法:
+ (NSString *)countdownTime:(NSTimeInterval)interval{
NSString *timeStr;
int second = (int)interval % 60;//秒
int minute = (int)interval / 60 % 60;
int hours = (int)interval / 3600 % 60;
int day = (int)interval / (24 *3600) % 60;
if (day != 0) {
timeStr = [NSString stringWithFormat:@"%d天%d:%d:%d",day,hours,minute,second];
}else if (day == 0 && hours != 0) {
timeStr = [NSString stringWithFormat:@"%d:%d:%d",hours,minute,second];
}else if (day == 0 && hours == 0 && minute != 0) {
timeStr = [NSString stringWithFormat:@"%d:%d",minute,second];
}else{
timeStr = [NSString stringWithFormat:@"%d",second];
}
return timeStr;
}
計(jì)時(shí)器不斷給控件刷新數(shù)據(jù),NSTimer我就不贅述了又活,效果如下: