1.
{
NSTimer *timer;
}
- (void)viewDidLoad {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeMethodGo:) userInfo:nil repeats:YES];
}
- (void)timeMethodGo:(NSTimer *)theTimer {
NSCalendar *calender = [NSCalendar currentCalendar];
NSDate *startDate = [NSDate date];
NSDateFormatter *foamatter = [[NSDateFormatter alloc] init];
[foamatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *endDate = [foamatter dateFromString:@"2016-10-25 00:00:00"];
unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ;
NSDateComponents *day = [calender components:unitFlags fromDate:startDate toDate:endDate options:0];
_dayLabel.text = [NSString stringWithFormat:@"%ld", (long)[day day]];
_hourLabel.text = [NSString stringWithFormat:@"%ld", (long)[day hour]];
_minuteLabel.text = [NSString stringWithFormat:@"%ld", (long)[day minute]];
_secondLabel.text = [NSString stringWithFormat:@"%ld", (long)[day second]];
if ([day day] == 0 && [day hour] == 0 && [day minute] == 0 && [day second] == 0) {
[theTimer setFireDate:[NSDate distantFuture]];
}
}
2.
{
dispatch_source_t _timer;
}
- (void)viewDidLoad {
NSDate *startDate = [NSDate date];
NSDateFormatter *foamatter = [[NSDateFormatter alloc] init];
[foamatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *endDate = [foamatter dateFromString:@"2017-10-24 00:00:00"];
NSTimeInterval timeInterval =[endDate timeIntervalSinceDate:startDate];
if (_timer==nil) {
__block int timeout = timeInterval; //倒计时时间
if (timeout!=0) {
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); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
if(timeout<=0){ //倒计时结束,关闭
dispatch_source_cancel(_timer);
_timer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
_dayLabel.text = @"";
_hourLabel.text = @"00";
_minuteLabel.text = @"00";
_secondLabel.text = @"00";
});
} else {
int days = (int)(timeout/(3600*24));
if (days==0) {
_dayLabel.text = @"";
}
int hours = (int)((timeout-days*24*3600)/3600);
int minute = (int)(timeout-days*24*3600-hours*3600)/60;
int second = timeout-days*24*3600-hours*3600-minute*60;
dispatch_async(dispatch_get_main_queue(), ^{
if (days==0) {
_dayLabel.text = @"0天";
} else {
_dayLabel.text = [NSString stringWithFormat:@"%d天",days];
}
if (hours<10) {
_hourLabel.text = [NSString stringWithFormat:@"0%d",hours];
} else {
_hourLabel.text = [NSString stringWithFormat:@"%d",hours];
}
if (minute<10) {
_minuteLabel.text = [NSString stringWithFormat:@"0%d",minute];
} else {
_minuteLabel.text = [NSString stringWithFormat:@"%d",minute];
}
if (second<10) {
_secondLabel.text = [NSString stringWithFormat:@"0%d",second];
} else {
_secondLabel.text = [NSString stringWithFormat:@"%d",second];
}
});
timeout--;
}
});
dispatch_resume(_timer);
}
}
}
}