关于倒计时

1,设定一个时间,当前时间与设定时间进行比较,1秒执行一次;

2,设定起始时间和结束时间,把当前时间和起始时间比较,当当前时间大于等于起始时间开始1步骤


self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];

    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];


- (void)timerFireMethod:(NSTimer*)theTimer {

    if (self.startTime && self.endTime) {

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

        NSDate *startDate = [dateFormatter dateFromString:self.startTime];

        NSDate *endDate = [dateFormatter dateFromString:self.endTime];


        if ([startDate compare:[self getNowDateFromatAnDate]] == NSOrderedAscending &&

            [endDate compare:[self getNowDateFromatAnDate]] == NSOrderedDescending) {

            NSArray *date = [self.endTime componentsSeparatedByString:@" "];

            if ([date count] > 1) {

                NSCalendar *cal = [NSCalendar currentCalendar];

                NSDateComponents *time = [[NSDateComponents alloc] init];

                

                NSArray *yearArr = [[date objectAtIndex:0] componentsSeparatedByString:@"-"];

                if ([yearArr count] == 3) {

                    [time setYear:[[yearArr objectAtIndex:0]intValue]];

                    [time setMonth:[[yearArr objectAtIndex:1]intValue]];

                    [time setDay:[[yearArr objectAtIndex:2]intValue]];

                }

                

                NSArray *timeArr = [[date objectAtIndex:1] componentsSeparatedByString:@":"];

                if ([timeArr count] == 3) {

                    [time setHour:[[timeArr objectAtIndex:0]intValue]];

                    [time setMinute:[[timeArr objectAtIndex:1]intValue]];

                    [time setSecond:[[timeArr objectAtIndex:2]intValue]];

                }

                NSDate *todate = [cal dateFromComponents:time];

                NSDate *today = [NSDate date];

                

                unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

                NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];

                int hour =[d hour];

                int minute = [d minute];

                int second = [d second];

                if (hour<=0 && minute<=0 && second<=0) {

                    self.timeLab.text = @"施工完成";

                    [self stop];

                }else {

                    NSString *h =[NSString stringWithFormat:@"%d",[d hour]];

                    NSString *m =[NSString stringWithFormat:@"%d",[d minute]];

                    NSString *s =[NSString stringWithFormat:@"%d",[d second]];

                    self.timeLab.text= [NSString stringWithFormat:@"%@:%@:%@",h.length!=1?h:[NSString stringWithFormat:@"0%@",h],m.length!=1?m:[NSString stringWithFormat:@"0%@",m],s.length!=1?s:[NSString stringWithFormat:@"0%@",s]];

                }

            }

        }

        if ([endDate compare:[self getNowDateFromatAnDate]] == NSOrderedAscending) {

            self.timeLab.text = @"施工完成";

            [self stop];

        }

        if ([startDate compare:[self getNowDateFromatAnDate]] == NSOrderedDescending) {

            self.timeLab.text = @"等待施工";

        }

    }

}


- (void)stop

{

    self.timeLab = nil;

    [self.timer invalidate];

    self.timer = nil;

}


- (NSDate *)getNowDateFromatAnDate

{

    NSDate *anyDate = [NSDate date];

    //设置源日期时区

    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

    //设置转换后的目标日期时区

    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];

    //得到源日期与世界标准时间的偏移量

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];

    //目标日期与本地时区的偏移量

    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];

    //得到时间偏移量的差值

    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

    //转为现在时间

    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];

    return destinationDateNow;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值