OC封装时间选择器
来源:互联网 发布:淘宝mg美即面膜 编辑:程序博客网 时间:2024/05/17 00:52
#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
- OC封装时间选择器
- 时间选择器的封装
- 时间选择器封装
- 封装一个日期时间选择器
- iOS 时间选择器封装(含三种模式)
- 封装$选择器
- 封装一个实用的日期时间选择器,时钟组件
- oc 封装
- OC 封装
- 封装所有版本兼容的Material风格的时间选择器和日期选择器
- 时间选择器
- 时间选择器
- 时间选择器
- 时间选择器
- 时间选择器
- 时间选择器
- 时间选择器
- 时间选择器
- DPDK之(五)——多核多线程机制简析
- 学习内容
- linux cp 复制
- springmvc ajax form表单提交出现400报错
- Matlab基础 主函数与子函数的调用关系
- OC封装时间选择器
- Java概念性问题10
- Use WEBP
- SylixOS 基于AT91SAM9X25的CAN总线传输流程解析
- vuejs组件使用问题
- 对于192.168.0.0到192.168.0.255这个网络来说,以下说法中正确的是
- openssl源码下载及编译
- Storm在Zookeeper中的目录结构
- [开源]excel文件自动代码配置生成器