LGLDatePickerView
这个是封装 系统的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的更多相关文章
随机推荐
- 学习之路三十六:SQL知识总结 - [游标||字符串分割]
好久没有写文章了,今天把前不久项目用到的SQL知识总结一下. 一丶字符串分割 SQL内置函数中是没有Split分割函数的,所以需要自己去实现,不多说,上代码: )) RETURNS @result T ...
- 日暮·第二章·烽烟传讯
第二章 烽烟传讯 夜幕降临,整个泉州府更见喧闹,那些个白日里将养了一日的花红柳绿再也耐不住寂寞,招招摇摇着在人来人往的主街上舒展着自己的风情,妖妖娆娆地换却春风一度. 城东的招福客栈在经过了 ...
- 替换GitBlit的证书为域证书
GitBlit(当前版本1.6.2,http://gitblit.org/) 是一个Git版本控制的服务端,使用java编写,功能上足够满足基本的版本控制要求,而且部署很简单方便,比如windows上 ...
- dnspod动态域名使用感受
继花生壳不能用之后,3322也开始不太好用了,首先就是360把所有3322的域名全部判定为危险域名,甚至拦截程序对于3322url的api请求. 所以想把3322换成我们自己的独立域名,但是3322他 ...
- MailMessage to EML
EML格式是微软公司在Outlook中所使用的一种遵循RFC822及其后续扩展的文件格式,并成为各类电子邮件软件的通用格式. 做个笔记,C# 邮件处理保存为eml格式: 一.网上好多这样的写法,可以在 ...
- css3 定义选择器
引言:CSS样式规则有两个主要部分.选择器决定将格式化应用到哪些元素.声明则定义要应用的格式化. 构造选择器 选择器可以定义五个不同的标准来选择要进行格式化的元素. 元素的类型或者名称.如下所示. h ...
- wordpress添加文章浏览统计(刷新不重复)
wordpress本身不带文章浏览统计,可以用插件wp-postview,但是刷新还是算一个浏览次数. 1.首先在主题下functions.php里增加以下代码,这段代码也是网上可以找到的 //add ...
- Subgradient Algorithm
Subgradient是一种可以优化不可微的凸函数的方法. 首先回顾凸函数的定义: $f(y) \geq f(x) + \nabla f(x)^T(y-x), all \hspace{2 pt} x, ...
- 【原创】试用十天被Pass所带来的启示
试用十天被Pass所带来的启示 招聘是门学问,很多人在研究,也有很多方案,不过面对人员难聘问题,很多方法又不灵了.于是我们采用了降低标准方案,扩招进来一些人员,于是问题又来了,想不想听我亲身经历的 ...
- 配置visual studio code进行asp.net core rc2的开发
1.安装.net core sdk https://github.com/dotnet/cli#installers-and-binaries,根据你的系统选择下载. 2.下载vscode的C#扩展插 ...