一.單例定時(shí)器


二.倒計(jì)時(shí)
1.自定義label
//GPLTimeLab.h
@interface GPLTimeLab : UILabel
/// 根據(jù)傳入的時(shí)間計(jì)算和當(dāng)前時(shí)間的進(jìn)行比較,開始倒計(jì)時(shí)
- (void)setupCountDownWithTargetTime:(NSDate *)targetTime;
/// 根據(jù)傳入的具體秒數(shù),開始倒計(jì)時(shí)
- (void)beginCountDownWithTimeInterval:(NSTimeInterval)timerInterval;
/// 根據(jù)傳入的兩個(gè)時(shí)間差,開始倒計(jì)時(shí)
- (void)beginCountDownFromTime:(NSDate *)fromDate toTime:(NSDate *)toDate;
@end
@interface GPLTimeLab ()
/// 定時(shí)器,使用 weak 或者 strong 都行
@property (nonatomic, strong) NSTimer *timer;
/// 剩余天數(shù)
@property (nonatomic, assign) NSUInteger day;
/// 剩余小時(shí)數(shù)
@property (nonatomic, assign) NSUInteger hour;
/// 剩余分鐘數(shù)
@property (nonatomic, assign) NSUInteger minute;
/// 剩余秒數(shù)
@property (nonatomic, assign) NSUInteger second;
@end
@implementation GPLTimeLab
/// 根據(jù)傳入的時(shí)間計(jì)算和當(dāng)前時(shí)間的進(jìn)行比較,開始倒計(jì)時(shí)
- (void)setupCountDownWithTargetTime:(NSDate *)targetTime {
// 計(jì)算傳入的時(shí)間和當(dāng)前的時(shí)間差
NSTimeInterval interval = [targetTime timeIntervalSinceDate:[NSDate date]];
[self initTimeParametersWithTimeInterval:(NSInteger)interval];
}
/// 根據(jù)傳入的具體秒數(shù),開始倒計(jì)時(shí)
- (void)beginCountDownWithTimeInterval:(NSTimeInterval)timerInterval {
[self initTimeParametersWithTimeInterval:timerInterval];
}
/// 根據(jù)傳入的兩個(gè)時(shí)間差,開始倒計(jì)時(shí)
- (void)beginCountDownFromTime:(NSDate *)fromDate toTime:(NSDate *)toDate {
NSTimeInterval interval = [fromDate timeIntervalSinceDate:toDate];
NSLog(@"%f",interval);
if (interval<0) {
NSLog(@"已完成");
self.text = @"已完成";
} else {
[self initTimeParametersWithTimeInterval:interval];
}
}
/// 通過傳入的時(shí)間間隔對時(shí)間參數(shù)進(jìn)行初始化
- (void)initTimeParametersWithTimeInterval:(NSInteger)interval {
NSUInteger secondPerDay = 24 * 60 * 60;
NSUInteger secondPerHour = 60 * 60;
NSUInteger secondPerMinute = 60;
// 計(jì)算天數(shù)
self.day = interval / secondPerDay;
// 剩余小時(shí)不應(yīng)該大于24小時(shí),所以應(yīng)該先除去滿足一天的秒數(shù),再計(jì)算還剩下多少小時(shí)
self.hour = interval % secondPerDay / secondPerHour;
// 剩余分鐘數(shù)與上面同理
self.minute = interval % secondPerHour / secondPerMinute;
// 剩余秒數(shù)直接等于秒數(shù)對每分鐘秒數(shù)所取的余數(shù)
self.second = interval % secondPerMinute;
// 更新值
[self updateText];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
/// 時(shí)間減一秒方法
- (void)updateTimer {
// 減一秒
self.second--;
// 判斷秒數(shù)
if (self.second == -1) {
self.second = 59;
self.minute--;
}
// 判斷分鐘數(shù)
if (self.minute == -1) {
self.minute = 59;
self.hour--;
}
// 判斷小時(shí)數(shù)
if (self.hour == -1) {
self.hour = 23;
self.day--;
}
// 判斷是否沒時(shí)間了
if (self.day == 0 && self.hour == 0 && self.minute == 0 && self.second == 0) {
[self.timer invalidate];
self.text = @"已完成";
self.timer = nil;
[self removeFromSuperview];
}
[self updateText];
NSLog(@"%s", __func__);
}
- (void)updateText {
if (self.day == 0 && self.hour == 0 && self.minute == 0 && self.second == 0)
{
self.text = @"已完成";
[self removeFromSuperview];
}
else
{
self.text = [NSString stringWithFormat:@"倒計(jì)時(shí):%02zd 天 %02zd:%02zd:%02zd", self.day, self.hour, self.minute, self.second];
}
}
// best solution
- (void)removeFromSuperview {
[super removeFromSuperview];
[self.timer invalidate];
}
// this solution don't work....
// - (void)dealloc {
//
//? ? [self.timer invalidate];
// }
@end
使用時(shí),每次進(jìn)入頁面從后臺獲取當(dāng)前時(shí)間和到期時(shí)間
//1、 獲得到期時(shí)間串
NSString *timSts = [NSString stringWithFormat:@"%@",[dataDic objectForKey:@"end_time"]];
NSLog(@"到期時(shí)間是:%@",timSts);
//獲取當(dāng)前時(shí)間串
nowDateStr = [NSString stringWithFormat:@"%@",[dataDic objectForKey:@"current_time"]];
NSLog(@"當(dāng)前時(shí)間是22:%@",nowDateStr);
[gpltimelab beginCountDownFromTime:[self creatDaoDate:timSts] toTime:[self creatDaoDate:nowDateStr]];
//字符串轉(zhuǎn)時(shí)間
-(NSDate *)creatDaoDate:(NSString *)Str
{
NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"YYYY-MM-dd HH:mm:ss";
format.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
format.timeZone = sourceTimeZone;
NSDate *date = [format dateFromString:Str];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:date];
NSDate *localDate = [date dateByAddingTimeInterval:interval];
NSLog (@"date is : %@", localDate );
return localDate;
}