iOS 一个可以输入时间的倒计时器

本文为博主手写总结性文章,如若涉及版权问题,请与博主联系。

#import "ViewController.h"

@interface ViewController () 

@property int Hour;
@property int Minute;
@property int Second;

@property int width;
@property int height;

@property NSTimer *lookforTimer;
@property UILabel *OutputTimelabel;
@property UITextField *InputTimelabel1;
@property UITextField *InputTimelabel2;
@property UITextField *InputTimelabel3;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //获取当前屏幕的宽高
    self.width = [UIScreen mainScreen].bounds.size.width;
    self.height = [UIScreen mainScreen].bounds.size.height;

    UILabel *TempLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 150, 50)];
    [TempLabel setText:@"请输入起始时间:"];
    [TempLabel setTextColor:[UIColor blackColor]];
    [self.view addSubview:TempLabel];
    
    self.InputTimelabel1 = [[UITextField alloc]initWithFrame:CGRectMake(200, 50, 50, 50)];
    [self.InputTimelabel1 setBorderStyle:UITextBorderStyleRoundedRect];
    self.InputTimelabel1.delegate = self;
    [self.view addSubview:self.InputTimelabel1];

    self.InputTimelabel2 = [[UITextField alloc]initWithFrame:CGRectMake(250, 50, 50, 50)];
    [self.InputTimelabel2 setBorderStyle:UITextBorderStyleRoundedRect];
    self.InputTimelabel2.delegate = self;
    [self.view addSubview:self.InputTimelabel2];
    
    self.InputTimelabel3 = [[UITextField alloc]initWithFrame:CGRectMake(300, 50, 50, 50)];
    [self.InputTimelabel3 setBorderStyle:UITextBorderStyleRoundedRect];
    self.InputTimelabel3.delegate = self;
    [self.view addSubview:self.InputTimelabel3];
    
    UIButton *btn =[[UIButton alloc]initWithFrame:CGRectMake(400, 50, 50, 50)];
    [btn setBackgroundColor:[UIColor redColor]];
    [btn setTitle:@"确认" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: btn];
    
    self.OutputTimelabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, self.width - 100,self.height - 100)];
    self.OutputTimelabel.font = [UIFont systemFontOfSize:130.0];
    [self.view addSubview:self.OutputTimelabel];

    [self CountdownMethod];
}

-(void)CountdownMethod{
    //数据初始化,先默认为0小时
    self.Hour = 0;
    self.Minute = 0;
    self.Second = 0;
    
    //每隔1秒调用一次
    self.lookforTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(CountdownAction) userInfo:nil repeats:YES];
}
-(void)CountdownAction{
    
    if (self.Hour == 0 && self.Minute == 0 && self.Second == 0){
    [self.lookforTimer invalidate];
    }else{
    self.Second -- ;
    //NSLog(@"Second---%d",self.Second);
    if (self.Second < 0) {
        self.Minute -- ;
        self.Second = 59;
        //NSLog(@"Minute---%d",self.Minute);
        if (self.Minute < 0) {
            self.Hour -- ;
            self.Minute = 59;
            //NSLog(@"Hour---%d",self.Hour);
            if (self.Hour < 0 && self.Minute < 0 && self.Second < 0) {
                [self.lookforTimer invalidate];
            }
        }
    }
    [self.OutputTimelabel setText:[NSString stringWithFormat:@"%d:%d:%d",self.Hour,self.Minute,self.Second]];
    }
}


-(void)btnAction{
    
    [self.view endEditing:YES];
    
    NSInteger temp1 = [self.InputTimelabel1.text integerValue];
    NSInteger temp2 = [self.InputTimelabel2.text integerValue];
    NSInteger temp3 = [self.InputTimelabel3.text integerValue];
    
    self.Hour = (int)temp1;
    self.Minute = (int)temp2;
    self.Second = (int)temp3;
    
    self.InputTimelabel1.text = nil;
    self.InputTimelabel2.text = nil;
    self.InputTimelabel3.text = nil;
    
    self.lookforTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(CountdownAction) userInfo:nil repeats:YES];
}

//控制输入的内容,只能输入数字。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number{
    BOOL res =YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i =0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i,1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length ==0) {
            res =NO;
            break;
        }
        i++;
    }
    return res;
}
@end

你可能感兴趣的:(iOS 一个可以输入时间的倒计时器)