美文网首页
UIDatePicker 使用

UIDatePicker 使用

作者: 通哥 | 来源:发表于2017-01-19 18:05 被阅读0次

#import "ViewController.h"

#define ScreenW [UIScreen mainScreen].bounds.size.width

#define ScreenH [UIScreen mainScreen].bounds.size.height

#define BXColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

@interface ViewController ()

@property (nonatomic, strong) UIDatePicker *datePicker;      // 选择器

@property (nonatomic, strong) UIButton *bgView;                // 背景视图

@property (nonatomic, strong) UIView *dateView;              // 日期视图

@property (nonatomic, strong) NSString *dateString;          // 日期字符

@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@property (nonatomic, strong) NSDate *date;                  // 日期

@property (nonatomic, strong) UIButton *sureButton;          // 取消按钮

@property (nonatomic, strong) UIButton *cancelButton;        // 取消按钮

@property (nonatomic, strong) UIButton *btn;

@end

@implementation ViewController

#pragma 懒加载

- (UIDatePicker *)datePicker{

if (_datePicker == nil) {

_datePicker = [[UIDatePicker alloc] init];

_datePicker.frame = CGRectMake(0, 0, ScreenW, 200);

_datePicker.backgroundColor = [UIColor whiteColor];

_datePicker.datePickerMode = UIDatePickerModeDate;

[_dateView addSubview:_datePicker];

}

return _datePicker;

}

- (UIView *)bgView{

if (_bgView == nil) {

_bgView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];

_bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.25];

[_bgView addTarget:self action:@selector(quXiaoBtn) forControlEvents:UIControlEventTouchUpInside];

_bgView.hidden = YES;

}

return _bgView;

}

- (UIView *)dateView{

if (_dateView == nil) {

_dateView = [[UIView alloc] init];

}

return _dateView;

}

- (NSString *)dateString{

if (_dateString == nil) {

_dateString = [[NSString alloc] init];

}

return _dateString;

}

- (NSDateFormatter *)dateFormatter{

if (!_dateFormatter) {

_dateFormatter = [[NSDateFormatter alloc] init];

[_dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

}

return _dateFormatter;

}

- (NSDate *)date{

if (!_date) {

_date = [[NSDate alloc] init];

}

return _date;

}

- (UIButton *)sureButton{

if (_sureButton == nil) {

_sureButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenW - 100*ScreenW/414, 250 - 40*ScreenH/736, 100*ScreenW/414, 40*ScreenH/736)];

_sureButton.layer.cornerRadius = 5.0;

[_sureButton setTitle:@"确定" forState:UIControlStateNormal];

[_sureButton addTarget:self action:@selector(shiJianQueRenBtn:) forControlEvents:UIControlEventTouchUpInside];

_sureButton.backgroundColor = BXColor(252, 102, 52);

}

return _sureButton;

}

- (UIButton *)cancelButton{

if (_cancelButton == nil) {

_cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 250 - 40*ScreenH/736, 100*ScreenW/414, 40*ScreenH/736)];

_cancelButton.layer.cornerRadius = 5.0;

[_cancelButton setTitle:@"取消" forState:UIControlStateNormal];

[_cancelButton addTarget:self action:@selector(quXiaoBtn) forControlEvents:UIControlEventTouchUpInside];

_cancelButton.backgroundColor = [UIColor lightGrayColor];

}

return _cancelButton;

}

- (void)viewDidLoad {

[super viewDidLoad];

self.dateString = [self.dateFormatter stringFromDate:self.date];

_btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 200, 40)];

_btn.center = self.view.center;

_btn.backgroundColor = [UIColor lightGrayColor];

[_btn setTitle:self.dateString forState:UIControlStateNormal];

[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_btn];

self.dateView.frame = CGRectMake(0, ScreenH, ScreenW, 250);

self.dateView.backgroundColor = BXColor(255, 255, 0);

[self.bgView addSubview:self.dateView];

[self.view addSubview:self.bgView];

[self.dateView addSubview:self.cancelButton];

[self.dateView addSubview:self.sureButton];

}

//取消按钮

- (void)quXiaoBtn{

[UIView animateWithDuration:0.25 animations:^{

self.dateView.frame = CGRectMake(0, ScreenH, ScreenW, 250);

} completion:^(BOOL finished) {

self.bgView.hidden = YES;

}];

}

- (void)shiJianQueRenBtn:(UIButton *)sender{

NSLog(@"您所选择的时间为:%@",self.dateString);

[self.btn setTitle:self.dateString forState:UIControlStateNormal];

[UIView animateWithDuration:0.25 animations:^{

self.dateView.frame = CGRectMake(0, ScreenH, ScreenW, 250);

} completion:^(BOOL finished) {

self.bgView.hidden = YES;

}];

}

#pragma make - 按钮点击

- (void)btnClick{

self.bgView.hidden = NO;

//背景View

[UIView animateWithDuration:0.25 animations:^{

self.dateView.frame = CGRectMake(0, ScreenH - 250, ScreenW, 250);

} completion:^(BOOL finished) {

}];

[self createRiQiPicker];

}

#pragma mark----------日期picker--------------------

- (void)createRiQiPicker{

[self.datePicker addTarget:self action:@selector(dateChange:) forControlEvents: UIControlEventValueChanged];

self.dateString = [self.dateFormatter stringFromDate:self.datePicker.date];

}

- (void)dateChange:(UIDatePicker *)senser{

self.dateString = [self.dateFormatter stringFromDate:self.datePicker.date];

}

@end

相关文章

网友评论

      本文标题:UIDatePicker 使用

      本文链接:https://www.haomeiwen.com/subject/tkiibttx.html