接上篇文章代码

云盘地址

ViewController.h

#import 

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
@property (weak, nonatomic) IBOutlet UILabel *lab3;
@property (weak, nonatomic) IBOutlet UILabel *lab4;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

//创建定时器(因为下面两个方法都使用,所以定时器拿出来设置为一个属性)
@property(nonatomic,strong)NSTimer * countDownTimer;//定时器
@property(nonatomic,strong)NSString *timerservice;//服务器保留时间
@property(nonatomic,strong)NSString *timetoday;//当前时间
@end

@implementation ViewController

static NSInteger  secondsCountDown ;    //倒计时总的秒数
    -(void) viewDidLoad {
    [super viewDidLoad];
    self.timetoday    = [self getCurrentTimes];//获取当前时间
    self.timerservice = @"17:40:00";//预设一个服务器记录的时间
    self.lab1.text = [NSString stringWithFormat:@"当前时间 %@"     ,self.timetoday   ];//当前时间
    self.lab2.text = [NSString stringWithFormat:@"服务器保留时间 %@",self.timerservice];//服务器保留时间
    
    [self pleaseInsertStarTimeo:self.timerservice andInsertEndTime:self.timetoday];
    
}

#pragma mark 获取当前时间
-(NSString*)getCurrentTimes{

     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:@"HH:mm:ss"];
    //现在时间,你可以输出来看下是什么格式
    NSDate *datenow = [NSDate date];
    //将nsdate按formatter格式转成nsstring
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    NSLog(@"currentTimeString   =  %@",currentTimeString);
    return currentTimeString;
}

#pragma mark 开始倒计时
 -(IBAction)click:(UIButton *)sender {
   _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1     target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
}

#pragma 计算2个时间差
-(void )pleaseInsertStarTimeo:(NSString *)time1 andInsertEndTime:(NSString *)time2{
    // 1.将时间转换为date
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"HH:mm:ss";
    NSDate *date1 = [formatter dateFromString:time1];
    NSDate *date2 = [formatter dateFromString:time2];
    // 2.创建日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit type  = NSCalendarUnitHour | NSCalendarUnitMinute |  NSCalendarUnitSecond;
    // 3.利用日历对象比较两个时间的差值
    NSDateComponents *cmps = [calendar components:type fromDate:date1 toDate:date2 options:0];
    // 4.输出结果
    NSLog(@"两个时间相差%ld小时%ld分钟%ld秒",cmps.hour, cmps.minute, cmps.second);
    self.lab3.text =[NSString stringWithFormat:@"两个时间相差%02ld小时%02ld分钟%02ld秒",cmps.hour, cmps.minute, cmps.second];
    secondsCountDown = cmps.hour*3600+cmps.minute*60+cmps.second;//给倒计时总的秒数赋值
    self.lab4.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",cmps.hour, cmps.minute, cmps.second];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

#pragma mark 实现倒计时动作
-(void)countDownAction{
    //倒计时-1
    secondsCountDown--;
    
    //重新计算 时/分/秒
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",secondsCountDown/3600];
    
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(secondsCountDown%3600)/60];
    
    NSString *str_second = [NSString stringWithFormat:@"%02ld",secondsCountDown%60];
    
    //修改倒计时标签及显示内容
    self.lab4.text = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    //当倒计时到0时做需要的操作,比如验证码过期不能提交
    if(secondsCountDown==0){
        
        [_countDownTimer invalidate];
    }
}
@end



接上篇文章代码_第1张图片
image.png

你可能感兴趣的:(接上篇文章代码)