IOS时间选择框

一、头文件中定义了委托方法

-(void)toobarDonBtnHaveClick:(ITTPickView *)pickView resultString:(NSString *)resultString;以及委托的对象delegate。注:委托方法是可选的。

#import <UIKit/UIKit.h>

@class ITTPickView;

@protocol ITTPickViewDelegate <NSObject>

@optional
-(void)toobarDonBtnHaveClick:(ITTPickView *)pickView
                resultString:(NSString *)resultString;

@end

@interface ITTPickView : UIView

@property(nonatomic,weak) id<ITTPickViewDelegate> delegate;//委托

/**
 *  通过时间创建一个DatePicker
 *
 *  @param date  默认选中时间
 *  @param isHaveNavControler是否在NavControler之内
 *
 *  @return 带有toolbar的datePicker
 */
-(instancetype)initDatePickWithDate:(NSDate *)defaulDate
                     datePickerMode:(UIDatePickerMode)datePickerMode
                 isHaveNavControler:(BOOL)isHaveNavControler;

/**
 *  从窗口移除本控件
 */
-(void)removeView;

/**
 *  在窗口显示本控件
 */
-(void)showView;

@end


二、ITTPickView的实现,主要的控件UIToolBar、UIDatePicker,点击确定后执行-(void)toobarDonBtnHaveClick:(ITTPickView *)pickView resultString:(NSString *)resultString(因为是可选的委托事件,实现了才会执行);获得选择的时间字符串。

修改:增加了蒙版视图,点击蒙版移除datePicker。

#import "ITTPickView.h"
#define ITTToobarHeight 40

@interface ITTPickView ()

@property (nonatomic,assign) NSDate *defaulDate;//默认时间
@property (nonatomic,strong) UIDatePicker *datePicker;//datePicker控件
@property (nonatomic,assign) NSInteger pickeviewHeight;//pickerView的高度
@property (nonatomic,strong) UIToolbar *toolbar;//toolBar控件
@property (nonatomic,copy) NSString *resultString;//返回的时间字符串
@property (nonatomic,assign) NSInteger selfOriginy;//当前view的frame.origin.y
@property (nonatomic,assign) NSInteger selfViewInitH;//初始状态view的frame.origin.y
@property (nonatomic,strong) UIView *dateView;//显示时间控件datePicker的view

@end

@implementation ITTPickView


//初始化ITTPickView,
- (instancetype)initDatePickWithDate:(NSDate *)defaulDate datePickerMode:(UIDatePickerMode)datePickerMode isHaveNavControler:(BOOL)isHaveNavControler {
    
    self = [super init];
    if (self) {
        
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        
        self.defaulDate = defaulDate;
        [self setUpDatePickerWithdatePickerMode:datePickerMode];
        [self setFrameWith:isHaveNavControler];
        [self setUpToolBar];
    }
    
    return self;
}


//设定ITTPickView的frame大小
-(void)setFrameWith:(BOOL)isHaveNavControler {
    
    CGFloat toolViewX = 0;
    CGFloat toolViewH = self.pickeviewHeight + ITTToobarHeight;
    CGFloat toolViewY;
    
    if (isHaveNavControler) {
        
        toolViewY = [UIScreen mainScreen].bounds.size.height - toolViewH - 50;
    }else {
        
        toolViewY = [UIScreen mainScreen].bounds.size.height - toolViewH;
    }
    
    CGFloat toolViewW = [UIScreen mainScreen].bounds.size.width;
    CGFloat toolViewInitH = [UIScreen mainScreen].bounds.size.height;
    self.selfViewInitH = toolViewInitH;//初始状态view的frame.origin.y
    self.selfOriginy = toolViewY;//当前view的frame.origin.y
    
    self.dateView.frame = CGRectMake(toolViewX, toolViewInitH, toolViewW, toolViewH);
    [self addSubview:self.dateView];
}


//设定datePicker控件的样式以及frame大小,并作为view的子视图
-(void)setUpDatePickerWithdatePickerMode:(UIDatePickerMode)datePickerMode {
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-CN"];
    datePicker.datePickerMode = datePickerMode;
    datePicker.backgroundColor = [UIColor whiteColor];
    
    if (self.defaulDate) {
        
        [datePicker setDate:self.defaulDate];
    }
    
    self.datePicker = datePicker;
    datePicker.frame = CGRectMake(0, ITTToobarHeight, [UIScreen mainScreen].bounds.size.width, datePicker.frame.size.height);
    self.pickeviewHeight = datePicker.frame.size.height;
    self.dateView = [[UIView alloc] initWithFrame:CGRectZero];//初始化dateView
    
    [self.dateView addSubview:datePicker];
}


//设置toolBar的各个属性,并作为view的子视图
- (void)setUpToolBar {
    
    self.toolbar = [self setToolbarStyle];
    [self setToolbarWithPickViewFrame];
    
    [self.dateView addSubview:self.toolbar];
}


//设置toolBar的样式
-(UIToolbar *)setToolbarStyle {
    
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@" 取消 " style:UIBarButtonItemStylePlain target:self action:@selector(removeView)];
    
    UIBarButtonItem *centerSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@" 确定 " style:UIBarButtonItemStylePlain target:self action:@selector(doneClick)];
    toolbar.items = @[left, centerSpace, right];
    
    return toolbar;
}


//设定tooBar的frame大小
- (void)setToolbarWithPickViewFrame {
    
    self.toolbar.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, ITTToobarHeight);
}


//点击确定按钮
-(void)doneClick {
    
    if (self.datePicker) {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd";
        self.resultString = [dateFormatter stringFromDate:self.datePicker.date];
    }
    
    if ([self.delegate respondsToSelector:@selector(toobarDonBtnHaveClick:resultString:)]) {
        
        [self.delegate toobarDonBtnHaveClick:self resultString:self.resultString];
    }
    
    [self removeView];
}


/**
 *  从窗口移除本控件
 */
- (void)removeView {
    
    [UIView animateWithDuration:0.25f animations:^{
        
        self.dateView.frame = CGRectMake(self.frame.origin.x, self.selfViewInitH, self.frame.size.width, self.frame.size.height);
        
    } completion:^(BOOL finished) {
        
        if (finished) {
            
            [self removeFromSuperview];
        }
    }];
}


/**
 *  在窗口显示本控件
 */
- (void)showView {

    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.25f animations:^{
        
        self.dateView.frame = CGRectMake(self.frame.origin.x, self.selfOriginy, self.frame.size.width, self.frame.size.height);
        
    } completion:^(BOOL finished) {
        
        
    }];
}


/**
 *  点击蒙版移除view
 */
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self removeView];
}


三、运用ITTPickView

UIButton *testBitton = [[UIButton alloc] initWithFrame:CGRectMake(0, 450, 111, 40)];
[testBitton setBackgroundColor:[UIColor redColor]];
  
[testBitton addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];


//点击按钮弹出时间选择框
- (void)test {
    
    ITTPickView *datePicker = [[ITTPickView alloc] initDatePickWithDate:[NSDate date] datePickerMode:UIDatePickerModeDate isHaveNavControler:NO];
    
    [datePicker showView];
}


你可能感兴趣的:(Datepicker)