聊天相關(guān)

一.單例定時(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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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