Cell (iOS form) – a simple implementation of a timer management 600 Countdown – Introduction to the 2 code

display http://www.jianshu.com/p/3c49b44e45b4
how to use: http://www.jianshu.com/p/5b4e0286658a
Download: https://github.com/zhYes/YSTimeCountDown

Cell (iOS form) - a simple implementation of a timer management 600 Countdown - Introduction to the 2 code
Facai – company adorable pet

This is the requirement for each Cell in the UITableView to add a countdown timer as shown below:

Cell (iOS form) - a simple implementation of a timer management 600 Countdown - Introduction to the 2 code

First, the countdown to achieve

First of all, this is the first time I would have thought of using the NSTimer timer, or GCD timer, or CADisplayLink timing it.

After a rough comparison here using the GCD timer

NSTimer is required to be used in the case of run loop has been enabled otherwise invalid. Only the main thread is the default startup runLoop. We can not guarantee that the method will not be written in the asynchronous case, so sometimes use NSTimer is not very safe. At the same time, NSTime pit is more, and the application of circular RunLoop pit that can open a special topic. The CADisplayLink is relatively suitable for the interface to do the re painting. NStimer is based on the implementation of RunLoop, but RunLoop is based on GCD to achieve, so that GCD can be considered more advanced.

In the model of.H tool package thought, here we provide a method for external calls

Requires 1 / / display countdown tableView | 2 end timestamp string array time - (void) countDownWithPER_SEC: (UITableView*): tableView (NSArray*) dataList;

Conventional code: frequently invoked by computing time display method

The incoming end time | / / calculate the difference with the current time - (NSString) getNowTimeWithString: (NSInteger): aTimeString (NSInteger) startTime{NSTimeInterval timeInterval = aTimeString - startTime; / / NSLog (@%f, timeInterval); int days = (int) (timeInterval/ (3600*24)); int (hours = (int) (timeInterval-days*24*3600) /3600); int minutes = (int) (timeInterval-days*24*3600-hours*3600) /60 int; seconds = timeInterval-days*24*3600-hours*3600-minutes*60; NSString *dayStr; NSString *hoursStr; NSString *minutesStr; NSString *secondsStr; dayStr = [NSString / day stringWithFormat:@ "%d" days] hoursStr [NSString stringWithFormat:@; / / hour = "%d" hours]; if (minutes< / / minutes 10 minutesStr = [NSStri) Ng stringWithFormat:@ "0%d" minutes] else [NSString stringWithFormat:@; minutesStr = "%d", minutes] if (seconds; / / < secondsStr = 10) [NSString stringWithFormat:@ 0%d, seconds] else [NSString stringWithFormat:@; secondsStr = "%d", seconds]; if (hours< =0& & minutes< =0& & seconds< =0) {return @ "event has ended! "(days);} if {return [NSString stringWithFormat:@ '% @% @% @% @ day hour points seconds", dayStr, hoursStr, minutesStr, secondsStr]; return [NSString stringWithFormat:@} "% @ h% @% @ divided seconds", hoursStr, minutesStr, secondsStr];}

The core code 1: interface to get the real server return time, and calculate the time difference between the current system and!

- (void) setupLess * Manager = [AFHTTPSessionManager {AFHTTPSessionManager manager]; [manager POST:@ app=life.time& appkey=10003& http://api.k780.com:88/? Sign=b59bc3ef6191eb9f747dd4e83c99f2a4& format=json parameters:nil progress:nil success:^ (NSURLSessionDataTask * _Nonnull task, NSDictionary* responseObject) {/ / NSLog (@ "% @", responseObject); NSString * webCurrentTimeStr = responseObject[@ "result"][@ "timestamp"]; NSInteger webCurrentTime = webCurrentTimeStr.longLongValue; date = NSDate * [NSDate date] NSInteger nowInteger; timeIntervalSince1970] = [date; _less = nowInteger - webCurrentTime; NSLog (@ "-- and server time difference --%zd, _less); If (_dataList) {[self destoryTimer]; [self countDownWithPER_SEC:_myTableView _dataList];}} failure:^ (NSURLSessionDataTask * _Nullable task NSError * _Nonnull error) {NSLog (@ "% @ - network error!", error);}];}

2: core code using the timer, a second change the current tableView visible cell need time countdown display using [NSDate date] per second and increasing end time difference this time is done, the transfer of the GCD timer displays the value of 1s, if we want the countdown time appears by the current server interface to obtain accurate time and time of mobile phone system make the difference, once calibrated our countdown! Instead of the frequent call interface

- (void): countDownWithPER_SEC (UITableView*): tableView (NSArray* dataList) {_myTableView = tableView; _dataList = dataList; if (_timer==nil) {dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); _timer = dispatch_source_create (DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer (_timer, dispatch_walltime (NULL, 0), 1.0*NSEC_PER_SEC, 0); / / per second dispatch_source_set_event_handler (_timer, dispatch_async (dispatch_get_main_queue), ^{(^{NSArray *cells = tableView.visibleCells ceLl for; / / remove screen is visible (UITableViewCell *cell in cells) {NSDate sjDate = [NSDate * date]; / / hand NSInteger sjInteger = [sjDate timeIntervalSince1970] time machine; / / mobile phone NSString* timestamp tempEndTime = dataList[cell.tag]; NSInteger = tempEndTime.longLongValue + endTime _less cell.textLabel.text = [self; getNowTimeWithString:endTime: sjInteger]; if ([cell.textLabel.text isEqualToString:@ "campaign has ended! The "[UIColor") {cell.textLabel.textColor = redColor];}else{= cell.textLabel.textColor [UIColor orangeColor];} / / NSLog (@ "% @ -", cell.detailTextLabel.text -% @ cell.textLabel.text);}});}); dispatch_resume (_timer); / / start timer}}

3: core code when others want to modify the local time countdown how to do?

[[NSNotificationCenter defaultCenter] selector:@selector (setupLess) name:UIApplicationDidBecomeActiveNotification addObserver:self object:nil];

Here only by the notification center to monitor our APP back into the foreground from the UIApplicationDidBecomeActiveNotification and re calculate the _less difference why?
well, it should not be explained

* / * * * / - active destruction timer (void) destoryTimer{if (_timer) {dispatch_source_cancel (_timer); _timer = nil;}}

The last section of the general code: the destruction of

Cell (iOS form) - a simple implementation of a timer management 600 Countdown - Introduction to the 2 code
to change the local time is still correct demonstration

========= any other problem, welcome message, is willing to work together with you to learn flash