这个是封装 系统的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. css3整理--filter

    只在chrome的少数版本的浏览器中有实现,兼容性差,不适宜应用. filter语法: elm { filter: none | <filter-function > [ <filt ...

  2. 自己开发的csdn手机客户端

    本人开发的,同步csdn官网新闻和博客内容,支持本地浏览,而且还可以手机上看到博客中的代码! 这是一款同步更新官网最新的资讯信息应用软件. 全新的用户界面,更好的用户体验,数据加载速度得到了进一步优化 ...

  3. cmd窗口编码方式的修改

    cmd默认的编码是采用GBK   regedit HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe\CodePage 十进制:936 GB ...

  4. nginx+lua_nginx+GraphicsMagick生成实时缩略图

    暂做笔记,带后续验证通过后,再补充 1.2.3 步. 一.安装 lua 首先确认是否安装 readline yum -y install readline-devel ncurses-devel 进入 ...

  5. Reactor模式与Proactor模式

    该文章总结了网上资源对这两种模式的描述 原文地址:http://www.cnblogs.com/dawen/archive/2011/05/18/2050358.html 1.标准定义 两种I/O多路 ...

  6. 【Java设计模式】单例模式

    ### 1. 概述> 单例模式是确保某一个类中有且只有一个实例. ----------### 2. 饿汉式单例``` javapublic class SingletonInstance { p ...

  7. pptv破解版程序,能够免费观看所有蓝光和会员影片!

    pptv破解版程序,能够免费观看所有蓝光和会员影片!PPTV网络电视3.4.1.0012绿色版(去广告本地vip版)由Black Hawk精简破解,去掉播放时缓冲.暂停广告.去掉迷你推荐和推荐弹窗.禁 ...

  8. ORA-12170:TNS:连接超时

    本文转自 http://www.cnblogs.com/kerrycode/archive/2012/12/14/2818421.html 1:首先检查网络是否能ping通 2:检查TNS配置(TNS ...

  9. 【转载】Linux下编辑生成.mo文件

    转载自:http://www.hackbase.com/tech/2012-02-27/65972.html 编辑生成.mo文件 我们在弄网站的时候很可能会接触到.mo和.po文件..po文件是GNU ...

  10. Network - 对比net-tools与iproute2

    net-tools VS iproute2 net-tools与iproute2大比较