前言
最近開發(fā)中摹察,用到了UITableViewCell
倒計時功能泽谨,這里將這部分功能分離出來,供大家參考帚桩。
1.原理
考慮到APP性能亿驾,這里只創(chuàng)建一個定時器,定時刷新當(dāng)前正在顯示的UITableViewCell
账嚎,使用Model
記錄剩余倒計時時間和當(dāng)前UITableViewCell
是否暫停颊乘。
2.核心代碼
創(chuàng)建定時器
考慮到方便和不需要銷毀参淹,這里定時器使用GCD--->GCD定時器封裝OC&Swift
self.timer = [[CLGCDTimer alloc] initWithInterval:1 delaySecs:0 queue:dispatch_get_main_queue() repeats:YES action:^(NSInteger actionTimes) {
__typeof(&*weakSelf) strongSelf = weakSelf;
strongSelf.actionTimes = actionTimes;
[strongSelf reloadVisibleCells];
}];
[self.timer start];
刷新當(dāng)前正在顯示的UITableViewCell
這里只對正在顯示的UITableViewCell
進(jìn)行操作醉锄,找出當(dāng)前正在顯示的UITableViewCell
對應(yīng)的數(shù)據(jù)Model
乏悄,對數(shù)據(jù)源進(jìn)行修改后刷新UITableView
。
- (void)reloadVisibleCells {
for (CLCountdownCell *cell in self.tableView.visibleCells) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
CLCountdownModel *model = [self.arrayDS objectAtIndex:indexPath.row];
model.actionTimes = self.actionTimes;
model.leaveTime = self.leaveTime;
if (model.isPause) {
continue;
}
cell.model = model;
}
}
Model數(shù)據(jù)修正
因?yàn)橹恍薷牧水?dāng)前正在顯示的UITableViewCell
對應(yīng)Model
的數(shù)據(jù)源恳不,所以滑動出來的UITableViewCell
對應(yīng)Model
數(shù)據(jù)源并不正確檩小,這里在UITableViewCell
即將顯示的時候修正對應(yīng)數(shù)據(jù)源。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
CLCountdownModel *model = [self.arrayDS objectAtIndex:indexPath.row];
model.actionTimes = self.actionTimes;
model.leaveTime = self.leaveTime;
if (!model.isPause) {
CLCountdownCell *countdownCell = (CLCountdownCell *)cell;
countdownCell.model = model;
}
}
記錄暫停狀態(tài)
通過修改對應(yīng)UITableViewCell
所在數(shù)據(jù)Model
烟勋,記錄當(dāng)前UITableViewCell
暫停狀態(tài)规求,達(dá)到暫停效果。這里需要注意記錄暫停當(dāng)時的時間以及重新開始的時間卵惦。
- (void)setIsPause:(BOOL)isPause {
if (isPause) {
self.pauseTime = self.remainingTime;
self.startTime = 0;
}else {
_isPause = isPause;
self.startTime = self.remainingTime;
}
_isPause = isPause;
}
- (NSInteger)remainingTime {
if (_isPause) {
return self.pauseTime;
}else {
if (self.pauseTime != 0 && self.startTime != 0) {
return self.countdownTime - self.actionTimes + self.pauseTime - self.startTime - self.leaveTime;
}else {
return self.countdownTime - self.actionTimes - self.leaveTime;
}
}
}
APP進(jìn)入后臺記錄
當(dāng)APP進(jìn)入后臺阻肿,需要記錄當(dāng)前時間,當(dāng)再次進(jìn)入前臺的時候沮尿,需要減去離開時間丛塌,這樣即使進(jìn)入后臺也不會影響到倒計時,這里考慮到進(jìn)入后臺期間修改時間等操作畜疾,直接使用系統(tǒng)運(yùn)行時間進(jìn)行記錄赴邻。
///系統(tǒng)當(dāng)前運(yùn)行了多長時間
///因?yàn)閮蓚€參數(shù)都會受用戶修改時間的影響,因此它們想減的值是不變的
+ (NSTimeInterval)uptimeSinceLastBoot {
//獲取當(dāng)前設(shè)備時間時間戳 受用戶修改時間影響
struct timeval now;
struct timezone tz;
gettimeofday(&now, &tz);
//獲取系統(tǒng)上次重啟的時間戳 受用戶修改時間影響
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
double uptime = -1;
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) {
//獲取上次啟動時間成功
//秒
uptime = now.tv_sec - boottime.tv_sec;
//微秒
uptime += (double)(now.tv_usec - boottime.tv_usec) / 1000000.0;
}
return uptime;
}
監(jiān)聽APP進(jìn)入后臺和進(jìn)入前臺通知啡捶,進(jìn)行記錄姥敛。
- (void)addNotification {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)applicationWillResignActive:(NSNotification *)notification {
self.resignSystemUpTime = [NSDate uptimeSinceLastBoot];
[self.timer suspend];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
self.becomeSystemUpTime = [NSDate uptimeSinceLastBoot];
self.leaveTime += (NSInteger)floor(self.becomeSystemUpTime - self.resignSystemUpTime);
[self.timer resume];
}
3.效果圖
這里數(shù)據(jù)源創(chuàng)建了10萬,因?yàn)橹挥幸粋€定時器瞎暑,并且只刷新當(dāng)前正在顯示的UITableViewCell
彤敛,所以滑動起來并不會有任何卡頓。
4.總結(jié)
UITableViewCell
倒計時代碼并不多了赌,只是需要注意一些細(xì)節(jié)墨榄,記錄對應(yīng)時間和狀態(tài)。更多細(xì)節(jié)請參考文章對應(yīng)Demo---->CLDemo如有幫助揍拆,歡迎Star渠概。