您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: