OC封装时间选择器
2017-05-15 14:36
162 查看
#import <UIKit/UIKit.h>
@protocol TimeDatePickerViewDelegate <NSObject>
//必须实现的两个协议
@required
- (void)changeTime : (NSDate *)date;//当时改变时出发
- (void)daterMine : (NSDate *)date;//更确定时间
@end
@interface TimeDatePickerView :UIView
//快速创建
+ (instancetype)datePickerWithType:(UIDatePickerMode) type ;
//初始化方法
- (instancetype)initWithFrame:(CGRect)frame type:(UIDatePickerMode)type;
//和代理营运而生的block
@property (nonatomic,copy)void(^changeTimeBlock)
(NSDate *date);
@property (nonatomic,copy)void(^determineBlock)
(NSDate *date);
//显示
- (void)show;
//设置初始时间
- (void)setNowTime:(NSString *)dateStr;
//可选的最大和最小时间
@property (nonatomic,strong)NSDate
*optionalMaxDate;
@property (nonatomic,strong)NSDate
*optionalMinDate;
//设置自定义标题
@property (nonatomic,copy)NSString
*title;
// NSDate --> NSString
- (NSString*)stringFromDate:(NSDate*)date;
//NSDate <-- NSString
- (NSDate*)dateFromString:(NSString*)dateString;
@property(assign ,nonatomic)id<TimeDatePickerViewDelegate>delegate;
@end
#import "TimeDatePickerView.h"
#define kZero 0
#define kFullWidth [UIScreen mainScreen].bounds.size.width
#define kFullHeight [UIScreen mainScreen].bounds.size.height
#define kDatePicY kFullHeight/3*2
#define kDatePicHeight kFullHeight/3
#define kDateTopBtnY kDatePicY - 30
#define kDateTopBtnHeight 30
#define kDateTopRightBtnWidth kDateTopLeftBtnWidth
#define kDateTopRightBtnX kFullWidth - 0 - kDateTopRightBtnWidth
#define kDateTopLeftbtnX 0
#define kDateTopLeftBtnWidth kFullWidth/6
@interface
TimeDatePickerView()
@property (nonatomic,strong)UIDatePicker
*dateP;
@property (nonatomic,strong)UIView
*groundV;
@property (nonatomic,strong)UIButton
*leftBtn;
@property (nonatomic,strong)UIButton
*rightBtn;
@property (nonatomic,strong)UIView
*topView;
@property (nonatomic,assign)UIDatePickerMode
type;
@property (nonatomic,strong)UILabel
*titleLabel;
@end
@implementation TimeDatePickerView
+ (instancetype)datePickerWithType:(UIDatePickerMode)type {
TimeDatePickerView *datePicker = [[TimeDatePickerViewalloc]
initWithFrame:[UIScreenmainScreen].boundstype:type];
return datePicker;
}
- (instancetype) initWithFrame:(CGRect)frame type:(UIDatePickerMode)type
{
self = [superinitWithFrame:frame];
if (self) {
self.type = type;
[selfaddSubview:self.groundV];
[selfaddSubview:self.dateP];
[selfaddSubview:self.topView];
[selfaddSubview:self.leftBtn];
[selfaddSubview:self.rightBtn];
}
returnself;
}
- (UIDatePicker *)dateP {
if (!_dateP) {
self.dateP = [[UIDatePickeralloc]
initWithFrame:CGRectMake(kZero,kDatePicY,
kFullWidth,kDatePicHeight)];
self.dateP.backgroundColor = [UIColorwhiteColor];
self.dateP.datePickerMode =self.type;
self.dateP.locale
= [[NSLocalealloc]
initWithLocaleIdentifier:@"zh_CHS_CN"];
[self.datePaddTarget:selfaction:@selector(handleDateP:)forControlEvents:UIControlEventValueChanged];
}
return_dateP;
}
- (UIView *)groundV {
if (!_groundV) {
self.groundV = [[UIViewalloc]initWithFrame:self.bounds];
self.groundV.backgroundColor = [UIColorclearColor];
self.groundV.alpha =0.7;
}
return_groundV;
}
//取消按钮
- (UIButton *)leftBtn{
if (!_leftBtn) {
self.leftBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
self.leftBtn.frame
= CGRectMake(kDateTopLeftbtnX,kDateTopBtnY,
kDateTopLeftBtnWidth,kDateTopBtnHeight);
[self.leftBtnsetTitle:@"取消"forState:UIControlStateNormal];
[self.leftBtnsetTitleColor:[UIColorwhiteColor]
forState:UIControlStateNormal];
// self.leftBtn.backgroundColor=[UIColor cyanColor];
[self.leftBtnaddTarget:selfaction:@selector(handleDateTopViewLeft)forControlEvents:UIControlEventTouchUpInside];
}
return_leftBtn;
}
//确定按钮
- (UIButton *)rightBtn {
if (!_rightBtn) {
self.rightBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
self.rightBtn.frame
= CGRectMake(kDateTopRightBtnX,kDateTopBtnY,
kDateTopRightBtnWidth,kDateTopBtnHeight);
[self.rightBtnsetTitleColor:[UIColorwhiteColor]
forState:UIControlStateNormal];
// self.rightBtn.backgroundColor=[UIColor cyanColor];
[self.rightBtnsetTitle:@"确定"forState:UIControlStateNormal];
[self.rightBtnaddTarget:selfaction:@selector(handleDateTopViewRight)forControlEvents:UIControlEventTouchUpInside];
}
return_rightBtn;
}
- (UIView *)topView {
if (!_topView) {
self.topView = [[UIViewalloc]initWithFrame:CGRectMake(kZero,kDateTopBtnY,
kFullWidth,kDateTopBtnHeight)];
self.topView.backgroundColor = [UIColorblackColor];
_titleLabel =[[UILabelalloc]initWithFrame:CGRectMake(0,0,
kFullWidth-2*(kDateTopLeftbtnX+kDateTopLeftBtnWidth)
, kDateTopBtnHeight)];
_titleLabel.text =@"选择时间";
_titleLabel.textAlignment =NSTextAlignmentCenter
;
_titleLabel.textColor =[UIColorwhiteColor];
_titleLabel.font = [UIFontsystemFontOfSize:15.0f];
_titleLabel.center =CGPointMake(_topView.frame.size.width/2,kDateTopBtnHeight/2);
[self.topViewaddSubview:_titleLabel];
}
return_topView;
}
- (void)setOptionalMaxDate:(NSDate *)optionalMaxDate{
_optionalMaxDate = optionalMaxDate;
self.dateP.maximumDate = optionalMaxDate;
}
- (void)setOptionalMinDate:(NSDate *)optionalMinDate{
_optionalMinDate = optionalMinDate;
self.dateP.minimumDate = optionalMinDate;
}
- (void)setTitle:(NSString *)title{
_title = title;
_titleLabel.text = title;
}
- (void)setNowTime:(NSString *)dateStr{
[self.datePsetDate:[selfdateFromString:dateStr]
animated:YES];
}
- (void)show{
[[UIApplicationsharedApplication].keyWindowaddSubview:self];
}
- (void)end{
[selfremoveFromSuperview];
}
- (void)handleDateP :(NSDate *)date {
if (self.changeTimeBlock) {
self.changeTimeBlock(self.dateP.date);
}
if ([self.delegaterespondsToSelector:@selector(changeTime:)])
{
[self.delegatechangeTime:self.dateP.date];
}
}
- (void)handleDateTopViewLeft {
[selfend];
}
- (void)handleDateTopViewRight {
if (self.determineBlock) {
self.determineBlock(self.dateP.date);
}
if ([self.delegaterespondsToSelector:@selector(determine:)])
{
[self.delegatedaterMine:self.dateP.date];
}
[selfend];
}
// NSDate --> NSString
- (NSString*)stringFromDate:(NSDate*)date{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]
init];
switch (self.type) {
caseUIDatePickerModeTime:
[dateFormatter setDateFormat:@"HH:mm"];
break;
caseUIDatePickerModeDate:
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
break;
caseUIDatePickerModeDateAndTime:
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
break;
caseUIDatePickerModeCountDownTimer:
[dateFormatter setDateFormat:@"HH:mm"];
break;
default:
break;
}
NSString *destDateString = [dateFormatterstringFromDate:date];
return destDateString;
}
//NSDate <-- NSString
- (NSDate*)dateFromString:(NSString*)dateString{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]
init];
switch (self.type) {
caseUIDatePickerModeTime:
[dateFormatter setDateFormat:@"HH:mm"];
break;
caseUIDatePickerModeDate:
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
break;
caseUIDatePickerModeDateAndTime:
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
break;
caseUIDatePickerModeCountDownTimer:
[dateFormatter setDateFormat:@"HH:mm"];
break;
default:
break;
}
NSDate *destDate= [dateFormatterdateFromString:dateString];
return destDate;
}
@end
@protocol TimeDatePickerViewDelegate <NSObject>
//必须实现的两个协议
@required
- (void)changeTime : (NSDate *)date;//当时改变时出发
- (void)daterMine : (NSDate *)date;//更确定时间
@end
@interface TimeDatePickerView :UIView
//快速创建
+ (instancetype)datePickerWithType:(UIDatePickerMode) type ;
//初始化方法
- (instancetype)initWithFrame:(CGRect)frame type:(UIDatePickerMode)type;
//和代理营运而生的block
@property (nonatomic,copy)void(^changeTimeBlock)
(NSDate *date);
@property (nonatomic,copy)void(^determineBlock)
(NSDate *date);
//显示
- (void)show;
//设置初始时间
- (void)setNowTime:(NSString *)dateStr;
//可选的最大和最小时间
@property (nonatomic,strong)NSDate
*optionalMaxDate;
@property (nonatomic,strong)NSDate
*optionalMinDate;
//设置自定义标题
@property (nonatomic,copy)NSString
*title;
// NSDate --> NSString
- (NSString*)stringFromDate:(NSDate*)date;
//NSDate <-- NSString
- (NSDate*)dateFromString:(NSString*)dateString;
@property(assign ,nonatomic)id<TimeDatePickerViewDelegate>delegate;
@end
#import "TimeDatePickerView.h"
#define kZero 0
#define kFullWidth [UIScreen mainScreen].bounds.size.width
#define kFullHeight [UIScreen mainScreen].bounds.size.height
#define kDatePicY kFullHeight/3*2
#define kDatePicHeight kFullHeight/3
#define kDateTopBtnY kDatePicY - 30
#define kDateTopBtnHeight 30
#define kDateTopRightBtnWidth kDateTopLeftBtnWidth
#define kDateTopRightBtnX kFullWidth - 0 - kDateTopRightBtnWidth
#define kDateTopLeftbtnX 0
#define kDateTopLeftBtnWidth kFullWidth/6
@interface
TimeDatePickerView()
@property (nonatomic,strong)UIDatePicker
*dateP;
@property (nonatomic,strong)UIView
*groundV;
@property (nonatomic,strong)UIButton
*leftBtn;
@property (nonatomic,strong)UIButton
*rightBtn;
@property (nonatomic,strong)UIView
*topView;
@property (nonatomic,assign)UIDatePickerMode
type;
@property (nonatomic,strong)UILabel
*titleLabel;
@end
@implementation TimeDatePickerView
+ (instancetype)datePickerWithType:(UIDatePickerMode)type {
TimeDatePickerView *datePicker = [[TimeDatePickerViewalloc]
initWithFrame:[UIScreenmainScreen].boundstype:type];
return datePicker;
}
- (instancetype) initWithFrame:(CGRect)frame type:(UIDatePickerMode)type
{
self = [superinitWithFrame:frame];
if (self) {
self.type = type;
[selfaddSubview:self.groundV];
[selfaddSubview:self.dateP];
[selfaddSubview:self.topView];
[selfaddSubview:self.leftBtn];
[selfaddSubview:self.rightBtn];
}
returnself;
}
- (UIDatePicker *)dateP {
if (!_dateP) {
self.dateP = [[UIDatePickeralloc]
initWithFrame:CGRectMake(kZero,kDatePicY,
kFullWidth,kDatePicHeight)];
self.dateP.backgroundColor = [UIColorwhiteColor];
self.dateP.datePickerMode =self.type;
self.dateP.locale
= [[NSLocalealloc]
initWithLocaleIdentifier:@"zh_CHS_CN"];
[self.datePaddTarget:selfaction:@selector(handleDateP:)forControlEvents:UIControlEventValueChanged];
}
return_dateP;
}
- (UIView *)groundV {
if (!_groundV) {
self.groundV = [[UIViewalloc]initWithFrame:self.bounds];
self.groundV.backgroundColor = [UIColorclearColor];
self.groundV.alpha =0.7;
}
return_groundV;
}
//取消按钮
- (UIButton *)leftBtn{
if (!_leftBtn) {
self.leftBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
self.leftBtn.frame
= CGRectMake(kDateTopLeftbtnX,kDateTopBtnY,
kDateTopLeftBtnWidth,kDateTopBtnHeight);
[self.leftBtnsetTitle:@"取消"forState:UIControlStateNormal];
[self.leftBtnsetTitleColor:[UIColorwhiteColor]
forState:UIControlStateNormal];
// self.leftBtn.backgroundColor=[UIColor cyanColor];
[self.leftBtnaddTarget:selfaction:@selector(handleDateTopViewLeft)forControlEvents:UIControlEventTouchUpInside];
}
return_leftBtn;
}
//确定按钮
- (UIButton *)rightBtn {
if (!_rightBtn) {
self.rightBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
self.rightBtn.frame
= CGRectMake(kDateTopRightBtnX,kDateTopBtnY,
kDateTopRightBtnWidth,kDateTopBtnHeight);
[self.rightBtnsetTitleColor:[UIColorwhiteColor]
forState:UIControlStateNormal];
// self.rightBtn.backgroundColor=[UIColor cyanColor];
[self.rightBtnsetTitle:@"确定"forState:UIControlStateNormal];
[self.rightBtnaddTarget:selfaction:@selector(handleDateTopViewRight)forControlEvents:UIControlEventTouchUpInside];
}
return_rightBtn;
}
- (UIView *)topView {
if (!_topView) {
self.topView = [[UIViewalloc]initWithFrame:CGRectMake(kZero,kDateTopBtnY,
kFullWidth,kDateTopBtnHeight)];
self.topView.backgroundColor = [UIColorblackColor];
_titleLabel =[[UILabelalloc]initWithFrame:CGRectMake(0,0,
kFullWidth-2*(kDateTopLeftbtnX+kDateTopLeftBtnWidth)
, kDateTopBtnHeight)];
_titleLabel.text =@"选择时间";
_titleLabel.textAlignment =NSTextAlignmentCenter
;
_titleLabel.textColor =[UIColorwhiteColor];
_titleLabel.font = [UIFontsystemFontOfSize:15.0f];
_titleLabel.center =CGPointMake(_topView.frame.size.width/2,kDateTopBtnHeight/2);
[self.topViewaddSubview:_titleLabel];
}
return_topView;
}
- (void)setOptionalMaxDate:(NSDate *)optionalMaxDate{
_optionalMaxDate = optionalMaxDate;
self.dateP.maximumDate = optionalMaxDate;
}
- (void)setOptionalMinDate:(NSDate *)optionalMinDate{
_optionalMinDate = optionalMinDate;
self.dateP.minimumDate = optionalMinDate;
}
- (void)setTitle:(NSString *)title{
_title = title;
_titleLabel.text = title;
}
- (void)setNowTime:(NSString *)dateStr{
[self.datePsetDate:[selfdateFromString:dateStr]
animated:YES];
}
- (void)show{
[[UIApplicationsharedApplication].keyWindowaddSubview:self];
}
- (void)end{
[selfremoveFromSuperview];
}
- (void)handleDateP :(NSDate *)date {
if (self.changeTimeBlock) {
self.changeTimeBlock(self.dateP.date);
}
if ([self.delegaterespondsToSelector:@selector(changeTime:)])
{
[self.delegatechangeTime:self.dateP.date];
}
}
- (void)handleDateTopViewLeft {
[selfend];
}
- (void)handleDateTopViewRight {
if (self.determineBlock) {
self.determineBlock(self.dateP.date);
}
if ([self.delegaterespondsToSelector:@selector(determine:)])
{
[self.delegatedaterMine:self.dateP.date];
}
[selfend];
}
// NSDate --> NSString
- (NSString*)stringFromDate:(NSDate*)date{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]
init];
switch (self.type) {
caseUIDatePickerModeTime:
[dateFormatter setDateFormat:@"HH:mm"];
break;
caseUIDatePickerModeDate:
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
break;
caseUIDatePickerModeDateAndTime:
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
break;
caseUIDatePickerModeCountDownTimer:
[dateFormatter setDateFormat:@"HH:mm"];
break;
default:
break;
}
NSString *destDateString = [dateFormatterstringFromDate:date];
return destDateString;
}
//NSDate <-- NSString
- (NSDate*)dateFromString:(NSString*)dateString{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]
init];
switch (self.type) {
caseUIDatePickerModeTime:
[dateFormatter setDateFormat:@"HH:mm"];
break;
caseUIDatePickerModeDate:
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
break;
caseUIDatePickerModeDateAndTime:
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
break;
caseUIDatePickerModeCountDownTimer:
[dateFormatter setDateFormat:@"HH:mm"];
break;
default:
break;
}
NSDate *destDate= [dateFormatterdateFromString:dateString];
return destDate;
}
@end
相关文章推荐
- Android WheelView 时间选择器 使用封装
- iOS 时间选择器封装(含三种模式)
- easyui的时间插件不能选择当天之前的时间
- 冒泡排序、选择排序、插入排序、快速排序算法的时间性能分析(java实现)
- 为什么计算机和一些电子产品的时间选择在1970.1.1
- LayDate 时间选择插件的使用介绍
- 时间日期选择
- Cognos Active Report 时间区间选择的解决办法
- fastText、TextCNN、TextRNN……这里有一套NLP文本分类深度学习方法库供你选择 作者:机器人圈 / 微信号:ROBO_AI发表时间 :2017-07-28 图:pixabay
- Scala - 处理时间(nscala-time - Joda Time的scala封装)
- Windows Phone 7 日期选择控件DatePicker和时间选择控件TimePicker
- ionic 日期和时间选择器
- Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker
- 一个在时间选择上很有用的代码
- HTML学习笔记之中的一个(input文件选择框的封装)
- 微信小程序使用picker实现时间和日期选择框功能【附源码下载】
- OC语言---封装,继承,多态
- OC面向对象—封装
- 封装顶部选择按钮(使用block可以在当前控制器下面添加方法)