这个是封装 系统的PickerView 使用也比较简单, gihub地址:https://github.com/liguoliangiOS/LGLDatePickerView.git

效果图

使用方法:

[[LGLDatePickerView shareDatePiker] datePikerShowWithMode:LGLDatePickerModeDate];

[[LGLDatePickerView shareDatePiker] dateCallBackSelectBlock:^(NSString *date) {

NSLog(@"%@", date);

}];

上代码:

LGLDatePickerView.h

//
// LGLDatePickerView.h
// LGLProgress
//
// Created by 李国良 on 2016/10/9.
// Copyright © 2016年 李国良. All rights reserved.
// #import <Foundation/Foundation.h> typedef NS_ENUM(NSInteger, LGLDatePickerrMode) {
LGLDatePickerModeTime, // 选择时间
LGLDatePickerModeDate, // 日期
LGLDatePickerModeDateAndTime, // 日期和时间
LGLDatePickerModeCountDownTimer, // 可以用于倒计时
}; /** 回调Block*/
typedef void(^DateSelectBlock)(NSString * date); @interface LGLDatePickerView : NSObject @property (nonatomic, copy) DateSelectBlock block;
+ (instancetype)shareDatePiker;
- (void)datePikerShowWithMode:(LGLDatePickerrMode)mode;
- (void)dateCallBackSelectBlock:(DateSelectBlock)block; @end

LGLDatePickerView.m

//
// LGLDatePickerView.m
// LGLProgress
//
// Created by 李国良 on 2016/10/9.
// Copyright © 2016年 李国良. All rights reserved.
// #import "LGLDatePickerView.h"
#import "UIView+Frame.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height @interface LGLDatePickerView () @property (nonatomic, strong) UIView * bgView;
@property (nonatomic, strong) UIView * dateView;
@property (nonatomic, strong) UIDatePicker * datePicker;
@property (nonatomic, copy) NSString * selectDate; @end @implementation LGLDatePickerView static id _instace;
+ (instancetype)shareDatePiker
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{// 只创建一次
_instace = [[self alloc] init];
});
return _instace;
} + (id)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{// 只初始化一次
_instace = [super allocWithZone:zone];
});
return _instace;
} - (id)copyWithZone:(NSZone *)zone
{
return _instace;
} - (void)datePikerShowWithMode:(LGLDatePickerrMode)mode {
if (!self.dateView) {
self.dateView = [[UIView alloc] initWithFrame:CGRectMake(, HEIGHT, WIDTH, )];
self.dateView.userInteractionEnabled = YES; // ============================= 添加分界线 =======================================================================
UIView * lineView1 = [[UIView alloc] initWithFrame:CGRectMake(, , WIDTH, )];
lineView1.backgroundColor = [UIColor colorWithRed: green: blue: alpha:0.5];
[self.dateView addSubview:lineView1];
UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(, , WIDTH, )];
lineView.backgroundColor = [UIColor colorWithRed: green: blue: alpha:0.5];
[self.dateView addSubview:lineView]; // ===================== 添加确定取消按钮 =========================================================================
UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cancelBtn.frame = CGRectMake(, , , );
[cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
cancelBtn.userInteractionEnabled = YES;
[cancelBtn addTarget:self action:@selector(cancelBtnclink) forControlEvents:UIControlEventTouchUpInside];
[self.dateView addSubview:cancelBtn]; UIButton * okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
okBtn.frame = CGRectMake(WIDTH - , , , );
[okBtn setTitle:@"确定" forState:UIControlStateNormal];
[okBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[okBtn addTarget:self action:@selector(okBtnClink) forControlEvents:UIControlEventTouchUpInside];
[self.dateView addSubview:okBtn]; // ==================== 初始化UIDatePicker,旋转滚动选择日期类 =======================================================
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , WIDTH, )];
[self.datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
[self.datePicker setTimeZone:[NSTimeZone localTimeZone]]; // 设置时区
[self.datePicker setDate:[NSDate date] animated:YES]; // 设置当前显示时间
//[datePicker setMaximumDate:[NSDate date]]; // 设置显示最大时间(此处为当前时间)最大和最小同时设置就不能选择未来的时间
UIDatePickerMode pikcerMode = (mode == LGLDatePickerModeDate) ? (UIDatePickerModeDate) : ((mode == LGLDatePickerModeDateAndTime) ? UIDatePickerModeDateAndTime : ((mode == LGLDatePickerModeTime) ? UIDatePickerModeTime : UIDatePickerModeCountDownTimer));
[self.datePicker setDatePickerMode:pikcerMode]; // 设置UIDatePicker的显示模式
[self.datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.dateView addSubview:self.datePicker];
UIWindow * window = [UIApplication sharedApplication].keyWindow;
[UIView animateWithDuration:0.5 animations:^{
self.dateView.y = HEIGHT - ;
}];
[window addSubview:self.dateView];
self.selectDate = [self dateToStringWithDate:[NSDate date]]; // 防止用户不选择日期直接选择确定
}
} - (void)cancelBtnclink {
[UIView animateWithDuration:0.5 animations:^{
self.dateView.y = HEIGHT;
}completion:^(BOOL finished) {
[self.dateView removeFromSuperview];
self.dateView = nil;
}];
} - (void)dateCallBackSelectBlock:(DateSelectBlock)block {
self.block = block;
} - (void)okBtnClink {
self.block(self.selectDate);
[self cancelBtnclink];
} - (void)datePickerValueChanged:(UIDatePicker *)datepiker {
self.selectDate = [self dateToStringWithDate:[datepiker date]];
} - (NSString *)dateToStringWithDate:(NSDate *)selectDate {
NSDateFormatter *selectDateFormatter = [[NSDateFormatter alloc] init];
selectDateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; // 设置时间和日期的格式
NSString * dateAndTime = [selectDateFormatter stringFromDate:selectDate]; // 把date类型转为设置好格式的string类型
return dateAndTime;
} @end

LGLDatePickerView的更多相关文章

随机推荐

  1. java ConcurrentModificationException探究

    当集合结构被修改,会抛出Concurrent Modification Exception. fail-fast会在以下两种情况下抛出ConcurrentModificationException ( ...

  2. 增大VM下linux的根目录空间

    增大VM下linux的根目录空间   用的太久,发现VM下的系统空间不足.简单的方法是,分一个新硬盘,挂载到根目录下.    下面是直接增大根目录下空间:  1. 增大vm下的磁盘大小, VM -&g ...

  3. Python基本语句

    x = 1 y = 1 dataf = 1.0 def fun1(): print("fun1") class ClassB(): def __init__(self): prin ...

  4. 谷歌面经 Tree Serialization

    http://www.careercup.com/question?id=4868040812396544 You should transform an structure of multiple ...

  5. The file 'MemoryStream' is corrupted! 的解决办法

    The file 'MemoryStream' is corrupted! Remove it and launch unity again! [Position > ] 有时候我们会遇到这个报 ...

  6. ps裁剪圆角

    1.打开要编辑的图片 2.选择圆角矩形工具,并调整半径(半径越大,角越圆),本例半径为20像素 3.使用上述工具画出选区 4.按下ctrl+enter,可以看到选区边缘描上了虚线 5.菜单栏-图像-剪 ...

  7. Legolas工业自动化平台入门(一)搭建应用

    前两篇给大家介绍了TWaver家族的新面孔--Legolas工业自动化平台,通过两个应用案例钻井平台工程用车和水源地监控系统,相信大家对Legolas已经有了一定程度的了解.这几篇文章,我们会逐步介绍 ...

  8. 【JAVA】"骗人"的“replaceAll”

    Java String类中有个方法叫:replaceAll,从表面上看,他的意思是把所有的regex替换成replacement. public String replaceAll(String re ...

  9. 指定的参数已超出有效值的范围 参数名: utcDate WebResource异常

    指定的参数已超出有效值的范围.参数名: utcDate 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.  异常详细信息 ...

  10. SNF开发平台WinForm之二-开发-单表表单管理页面-SNF快速开发平台3.3-Spring.Net.Framework

    2.1运行效果: 2.2开发实现: 2.2.1 这个开发与第一个开发操作步骤是一致的,不同之处就是在生成完代码之后,留下如下圈红程序,其它删除. 第一个开发地址:开发-单表表格编辑管理页面 http: ...