UIDatePicker的运用

来源:互联网 发布:编程工具排行榜 编辑:程序博客网 时间:2024/05/17 03:21

- (IBAction)SetTime1:(UIButton *)sender {

    

    [selfSelectTime1];

}


-(void)SelectTime1{

    UIView *bgView = [[UIViewalloc] initWithFrame:self.view.bounds];

    _pickBgView = bgView;

    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(removeBgView)];

    bgView.userInteractionEnabled =YES;

    [bgView addGestureRecognizer:tapGes];

    [self.viewaddSubview:bgView];

    

    UIDatePicker *datePicker; datePicker = [[UIDatePickeralloc] initWithFrame:CGRectMake(0,ViewHeight+20,ViewWidth, 150)];

    datePicker.datePickerMode =UIDatePickerModeDate;

    datePicker.date = [NSDatedate];

    [_pickBgView addSubview:datePicker];

    

    //添加事件

    [datePicker addTarget:selfaction:@selector(clickDatePicker1:)forControlEvents:UIControlEventValueChanged];


    //设置最小日期为现在的日期

    [datePicker setMinimumDate:[NSDatedate]];

    //设置最大日期为202001-01

    NSDateFormatter *format = [[NSDateFormatteralloc] init];

    [format setDateFormat:@"yyyy-MM-dd"];

    NSDate *MaxDate = [format dateFromString:@"2020-01-01"];

    [datePicker setMaximumDate:MaxDate];

  

    _datePicker = datePicker;

    //设置为中文显示

    NSLocale *locale = [[NSLocalealloc] initWithLocaleIdentifier:@"zh_CN"];

    datePicker.locale = locale;

    datePicker.backgroundColor = [UIColorwhiteColor];

    [UIViewanimateWithDuration:0.5animations:^{

    datePicker.frame = CGRectMake(0, ViewHeight-80, ViewWidth, 150);

    }];

    

}


#pragma mark - 点击时间选择器

- (void)clickDatePicker1:(UIDatePicker *)sender

{

    NSDate *date = sender.date;

    NSDateFormatter *format = [[NSDateFormatteralloc] init];

   // [format setDateFormat:@"yyyy-MM-dd"];

    [format setDateFormat:@"MMdd"];

    

     NSString *dateString = [format stringFromDate:date];

    

    //设置后台传递的时间戳

    NSString *timeSp = [NSStringstringWithFormat:@"%ld",(long)[sender.datetimeIntervalSince1970]];

    

    //改变日期的字符串数据时间戳参数

    NSLog(@"dateString %@",timeSp);

   

    // 设置界面显示的文字

    self.TimeLab1.text=dateString;

 }



#pragma mark - 移除选择时间视图

- (void)removeBgView

{

    [UIViewanimateWithDuration:0.5animations:^{

        _datePicker.frame =CGRectMake(0,ViewHeight+100,ViewWidth, 150);

        

    [selfperformSelector:@selector(afterAction)withObject:nilafterDelay:1];


    }];


}


//移除PickerView

- (void)removePickerView

{

    [UIViewanimateWithDuration:0.5animations:^{

        _pickView.frame =CGRectMake(0,ViewHeight+100,ViewWidth, 150);

        

    [selfperformSelector:@selector(afterAction)withObject:nilafterDelay:1];

    }];

    //移除视图

}


- (void)afterAction

{

    [_pickBgViewremoveFromSuperview];

}


0 0
原创粉丝点击