一,NSTimer
-(void)viewDidLoad{
[superviewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
timeLabel =[[UILabelalloc] initWithFrame:CGRectMake(0,200, self.view.frame.size.width,40)];
timeLabel.backgroundColor = [UIColorcyanColor];
[self.viewaddSubview:timeLabel];
// 创建一个NSTimer类
[NSTimer scheduledTimerWithTimeInterval:1.0target:selfselector:@selector(timeMethodGo:)userInfo:nilrepeats:YES];
}
//第一种
//-(void)timeMethodGo:(NSTimer *)timer{
//
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
// NSString *timeStr1 = @"2016-05-25 15:35:20";
// NSDate *date1 = [formatter dateFromString:timeStr1];
// NSString *dateStr = [formatter stringFromDate:[NSDate date]];
// NSDate *date = [formatter dateFromString:dateStr];
// NSTimeInterval tt = [date1 timeIntervalSinceDate:date];
// int d=tt/(3600*24);
// int h=(tt-d*3600*24)/3600;
// int m=(tt-d*3600*24-h*3600)/60;
// int s=tt-d*3600*24-h*3600-m*60;
// timeLabel.text=[NSString stringWithFormat:@"距离结束时间 :%d天%d时%d分%d秒",d,h,m,s];
//
//}
//第二种
-(void)timeMethodGo:(NSTimer *)timer{
NSDateFormatter *formatter = [[NSDateFormatteralloc] init];
formatter.dateFormat =@"yyyy-MM-dd HH:mm:ss";
formatter.locale = [[NSLocalealloc] initWithLocaleIdentifier:@"zh_CN"];
NSString *timeStr1 =@"2017-05-26 10:35:20";
NSDate *date1 = [formatter dateFromString:timeStr1];
NSString *dateStr = [formatter stringFromDate:[NSDate date]];
NSDate *date = [formatter dateFromString:dateStr];
unsignedint unitFlags =NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit | NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSCalendar *calendar = [NSCalendarcurrentCalendar];
//计算时间差
NSDateComponents *d = [calendar components:unitFlags fromDate:date toDate:date1 options:0];
timeLabel.text=[NSStringstringWithFormat:@"距离结束时间 :%ld年%ld月%ld天%ld时%ld分%ld秒",(long)[dyear],(long)[dmonth],(long)[dday],(long)[dhour],(long)[dminute],(long)[dsecond]];
}
- (void)viewDidLoad {
[superviewDidLoad];
UILabel *timeLabel = [[UILabelalloc] initWithFrame:CGRectMake(10,200, self.view.frame.size.width-20,40)];
timeLabel.backgroundColor = [UIColoryellowColor];
[self.viewaddSubview:timeLabel];
//当前时间
NSDate *date = [NSDatedate];
NSDateFormatter *formatter = [[NSDateFormatteralloc] init];
formatter.dateFormat =@"yyyy-MM-dd HH:mm:ss";
formatter.locale = [[NSLocalealloc] initWithLocaleIdentifier:@"zh_CN"];
NSString *str = [formatter stringFromDate:date];
NSLog(@"str = %@",str);
//2016.5.26 10:35:20 - 2016.6.15 12:20:35;倒计时
//取后台给的当前时间和结束时间(这里自己确定)
NSString *timeStr1 =@"2016-05-26 10:35:20";
NSString *timeStr2 =@"2016-05-26 10:35:25";
NSDate *date1 = [formatter dateFromString:timeStr1];
NSDate *date2 = [formatter dateFromString:timeStr2];
NSTimeInterval interval = [date2timeIntervalSinceDate:date1];
__block float timeout= interval;//倒计时时间
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_source_t _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);
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示根据自己需求设置
timeLabel.text = [NSStringstringWithFormat:@"活动已经结束"];
});
}else{
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示根据自己需求设置
//==================================================================================//
// NSString *hStr;
// NSString *mStr;
// NSString *sStr;
// NSString *dStr;
// NSInteger num;
// if (timeout/86400>1) {
// dStr = [NSString stringWithFormat:@"%f",(float)timeout/86400];
// dStr = [dStr substringToIndex:dStr.length - 7];
// dStr = [NSString stringWithFormat:@"%@",dStr];
// num = [dStr integerValue];
// NSTimeInterval interval1 = timeout - 86400*num;
// if (interval1/3600>1) {
// hStr = [NSString stringWithFormat:@"%f",interval1/3600];
// hStr = [hStr substringToIndex:hStr.length - 7];
// hStr = [NSString stringWithFormat:@"%@",hStr];
// }
// num = [hStr integerValue];
// NSTimeInterval interval2 = interval1 - 3600*num;
// if (interval2/3600 < 1 && interval2/60 > 1) {
// mStr = [NSString stringWithFormat:@"%f",interval2/60];
// mStr = [mStr substringToIndex:mStr.length-7];
// mStr = [NSString stringWithFormat:@"%@",mStr];
// }
// num = [mStr integerValue];
// NSTimeInterval interval3 = interval2 - 60*num;
// if (interval3 >= 0) {
// sStr = [NSString stringWithFormat:@"%f",interval3];
// sStr = [sStr substringToIndex:sStr.length-7];
// if (sStr == NULL) {
// sStr = @"0";
// }
// }
//
// timeLabel.text = [NSString stringWithFormat:@"相差%@天%@小时%@分%@秒",dStr,hStr,mStr,sStr];
//
// }
// if (timeout/86400<1 && timeout/3600>1) {
// NSTimeInterval interval1 = timeout;
// hStr = [NSString stringWithFormat:@"%f",interval1/3600];
// hStr = [hStr substringToIndex:hStr.length - 7];
// hStr = [NSString stringWithFormat:@"%@",hStr];
// num = [hStr integerValue];
// NSTimeInterval interval2 = interval1 - 3600*num;
// if (interval2/3600 < 1 && interval2/60 > 1) {
// mStr = [NSString stringWithFormat:@"%f",interval2/60];
// mStr = [mStr substringToIndex:mStr.length-7];
// mStr = [NSString stringWithFormat:@"%@",mStr];
// }
// num = [mStr integerValue];
// NSTimeInterval interval3 = interval2 - 60*num;
// if (interval3 >= 0) {
// sStr = [NSString stringWithFormat:@"%f",interval3];
// sStr = [sStr substringToIndex:sStr.length-7];
// if (sStr == NULL) {
// sStr = @"0";
// }
// }
// timeLabel.text = [NSString stringWithFormat:@"相差%@小时%@分%@秒",hStr,mStr,sStr];
// }
// if (timeout/3600<1&&timeout/60 > 1) {
// NSTimeInterval interval2 = timeout;
// mStr = [NSString stringWithFormat:@"%f",interval2/60];
// mStr = [mStr substringToIndex:mStr.length-7];
// mStr = [NSString stringWithFormat:@"%@",mStr];
// num = [mStr integerValue];
// NSTimeInterval interval3 = interval2 - 60*num;
// if (interval3 >= 0) {
// sStr = [NSString stringWithFormat:@"%f",interval3];
// sStr = [sStr substringToIndex:sStr.length-7];
// if (sStr == NULL) {
// sStr = @"0";
// }
// }
// timeLabel.text = [NSString stringWithFormat:@"相差%@分%@秒",mStr,sStr];
// }
// if (timeout/60 < 1 && timeout >= 0) {
// NSTimeInterval interval3 = timeout;
// sStr = [NSString stringWithFormat:@"%f",interval3];
// sStr = [sStr substringToIndex:sStr.length-7];
// if (sStr == NULL) {
// sStr = @"0";
// }
// timeLabel.text = [NSString stringWithFormat:@"相差%@秒",sStr];
// }
//==================================================================================//
int d=timeout/(3600*24);
int h=(timeout-d*3600*24)/3600;
int m=(timeout-d*3600*24-h*3600)/60;
int s=timeout-d*3600*24-h*3600-m*60;
timeLabel.text=[NSStringstringWithFormat:@"距离结束时间 :%d天%d时%d分%d秒",d,h,m,s];
});
timeout--;
}
});
dispatch_resume(_timer);
}