UIDatePicker封装
#import <UIKit/UIKit.h>
#import <objc/runtime.h> @protocol datePickerViewDelegate <NSObject> -(void)datePickerViewChoseTime:(NSString *)timeStr; @end @interface DatePickerView : UIView
@property(nonatomic,strong)UIDatePicker *datePicker;
@property(nonatomic,strong)UILabel *datePickerTimeLab;
@property(nonatomic,strong)NSDate *selectDate;
@property(nonatomic,assign)id<datePickerViewDelegate>delegate;
@property(nonatomic,copy)void(^hideBlock)();// 隐藏时调用的block -(void)showDatePickerView;
-(void)dismissDatePickerView;
@end
#import "DatePickerView.h"
@interface DatePickerView ()
{
UIView *backView;
}
@end @implementation DatePickerView -(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:CGRectMake(0, 0, MAINSCROON_WIDTH, MAINSCROON_HEIGHT)];
if(self){
backView = [UIView new];
[self addSubview:backView];
backView.backgroundColor = [UIColor blackColor];
backView.alpha = 0.25;
[backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(self);
make.top.offset(0);
make.left.offset(0);
}];
self.datePicker = [UIDatePicker new];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中
self.datePicker.locale = locale;
self.datePicker.backgroundColor = [UIColor whiteColor];
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[self.datePicker addTarget:self action:@selector(dataValueChanged:) forControlEvents:UIControlEventValueChanged];
[self addSubview:self.datePicker];
[self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mas_centerX);
make.bottom.offset(-(BOTTOM_HEIGHT));
if([UIScreen mainScreen].bounds.size.width >= 720){
make.height.offset(320);
}else{
make.height.offset(180);
}
make.width.equalTo(self.mas_width);
}]; UIView *topView = [UIView new];
topView.backgroundColor = [UIColor whiteColor];
[self addSubview:topView];
[CreateUI addFrameWithBottomView:topView borderHeight:1.0f withBorderColor:BORDER_COLOR];
[topView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.datePicker.mas_top).offset(0);
make.width.equalTo(self.datePicker.mas_width);
make.height.offset(34);
make.left.offset(0);
}];
UILabel *choseLab = [UILabel new];
choseLab.text = @"选择时间:";
choseLab.font = [UIFont systemFontOfSize:14];
[topView addSubview:choseLab];
[choseLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(topView.mas_centerY);
make.left.offset(5);
}]; self.datePickerTimeLab = [UILabel new];
self.datePickerTimeLab.font = [UIFont systemFontOfSize:14];
[self dataValueChanged:self.datePicker];
[topView addSubview:self.datePickerTimeLab];
[self.datePickerTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(topView.mas_centerY);
make.left.equalTo(choseLab.mas_right).offset(5);
}]; UIButton *sureBtn = [UIButton new];
sureBtn.clipsToBounds = YES;
sureBtn.layer.cornerRadius = 17;
[sureBtn setTitle:@"确定" forState:UIControlStateNormal];
[self addSubview:sureBtn];
sureBtn.backgroundColor = [ImageService colorWithHexString:@"#12b7f5"]; [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.datePicker.mas_bottom).offset(-10);
make.centerX.equalTo(self.datePicker.mas_centerX);
make.height.offset(34);
make.width.equalTo(self.datePicker.mas_width).multipliedBy(0.7);
}];
[sureBtn addTarget:self action:@selector(sureTimeBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void)sureTimeBtnClick{ [_delegate datePickerViewChoseTime:self.datePickerTimeLab.text]; [self dismissDatePickerView];
}
- (void) dataValueChanged:(UIDatePicker *)sender
{
UIDatePicker *dataPicker_one = (UIDatePicker *)sender;
NSDate *date_one = dataPicker_one.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
self.datePickerTimeLab.text = [formatter stringFromDate:date_one];
}
-(void)setSelectDate:(NSDate *)selectDate{
_selectDate = selectDate;
self.datePicker.date = selectDate;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
self.datePickerTimeLab.text = [formatter stringFromDate:selectDate];
} -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self dismissDatePickerView];
} -(void)showDatePickerView{
UIWindow *window = [[[UIApplication sharedApplication]delegate]window];
[window endEditing:YES];
[window addSubview:self];
self.alpha = 0;
[UIView animateWithDuration:0.3 animations:^{
self.alpha = 1;
}];
}
-(void)dismissDatePickerView{
if(self.hideBlock){
self.hideBlock();
}
UIWindow *window = [[[UIApplication sharedApplication]delegate]window];
[window addSubview:self];
self.alpha = 1;
[UIView animateWithDuration:0.3 animations:^{
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}]; } @end
使用:
//懒加载
-(DatePickerView *)datePickerView{
if(!_datePickerView){
_datePickerView = [DatePickerView new];
_datePickerView.delegate =self;
_datePickerView.datePicker.minimumDate = [NSDate date];
}
return _datePickerView;
}
//显示 如果设置默认日期可以这个 [self.datePickerView.datePicker setDate:[NSDate date]];
[self.datePickerView showDatePickerView];
UIDatePicker封装的更多相关文章
- 自定义一个只显示年月的DatePicker(UIDatePicker无法实现年月显示)
HooDatePicker 介绍(introduction) ==================================================项目需要一个DatePicker,只显 ...
- iOS开发--UIDatePicker
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
- UIDatePicker 日期/时间选取器(滚轮)—IOS开发
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
- IOS开发—UIDatePicker 日期/时间选取器(滚轮)
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
- iOS开发——高级UI之OC篇&UIdatePicker&UIPickerView简单使用
UIdatePicker&UIPickerView简单使用 /***************************************************************** ...
- IOS中UIDatePicker
UIDatePicker 1.常见属性 /* 样式 UIDatePickerModeTime,时间 UIDatePickerModeDate,日期 UIDatePickerModeDateAndTim ...
- iOS基础 - UIDatePicker and UIPickerView and UITextField
1.UIDatePicker继承自UIControl,因此不会通过代理来监听事件的改变,而是通过addTarget来监听事件.监听的事件是值改变事件. 2.UIPickerView继承自UIView, ...
- UIDatePicker的使用
UIDatePicker的介绍 UIDatePicker这个类的对象让用户可以在多个车轮上选择日期和时间.iPhone手机上的‘时钟’应用程序中的时间与闹铃中便使用了该控件.使用这个控件时,如果你能配 ...
- 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )
转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...
随机推荐
- Git合并最近的commit
合并commit的做法一般用在pull request的时候,把开发同一功能时的所有琐碎的commit合并到一个(假装自己的代码是高质量代码(手动滑稽)).主要使用的命令是git rebase 或者g ...
- Javascript--数组转换成字符串
定义和用法 toString() 方法可把数组转换为字符串,并返回结果. 语法 arrayObject.toString() 返回值 arrayObject 的字符串表示.返回值与没有参数的 join ...
- 整型数字转utf8
static std::string codePointToUTF8(unsigned int cp){ std::string result; // based on description fro ...
- ElasticSearch5.X的冷热数据架构
转载:https://my.oschina.net/xiaomaijiang/blog/826701 当使用ElasticSearch做大规模的时序数据分析的时候,我们建议使用基于时序的索引并且采用3 ...
- D - Wireless Network
来源poj2236 An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have s ...
- 【openstack N版】——网络服务neutron(flat扁平网络)
一.openstack网络服务neutron 1.1neutron介绍 neutron是openstack重要组件之一,在以前是时候没有neutron项目,早期的时候是没有neutron,早期所使用的 ...
- PAT甲级1131 Subway Map【dfs】【输出方案】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432 题意: 告诉你一个地铁线路图,站点都是 ...
- LUA 语言易混点
--代码: tab1 = { key1 = "val1", key2 = "val2","val2", "val3" , ...
- [No0000110]Git9/9-自定义Git
在安装Git一节中,我们已经配置了user.name和user.email,实际上,Git还有很多可配置项. 比如,让Git显示颜色,会让命令输出看起来更醒目: $ git config --glob ...
- [No0000104]JavaScript-基础课程4
要说 JavaScript 和其他较为常用的语言最大的不同是什么,那无疑就是 JavaScript 是函数式的语言,函数式语言的特点如下: 函数为第一等的元素,即人们常说的一等公民.就是说,在函数式编 ...