ios 自定义时间选择器

typedef NS_ENUM(NSInteger, UIDatePickerMode) {
    UIDatePickerModeTime,           // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
    UIDatePickerModeDate,           // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)
    UIDatePickerModeDateAndTime,    // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
    UIDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)
}

官方的UIDatePicker只有四种模式,不能选到秒。
变态的需求必须要精确到秒,就自定义了一个。

思路

定义多个UIPickerView,用来选择年-月-日 时:分:秒。注意tag区分每个pickerView,设置好dataSource和delegate
每个pickerView显示1个component,多行row供选择。

.h

#import 

@protocol WJDatePickerDelegate;
@interface WJDatePicker : UIView

@property (nonatomic, retain) NSDate*   date;       // 当前date
@property (nonatomic, strong) NSString* dateString;

/**
 * 设置默认值为当前时间
 */
-(void)resetDateToCurrentDate;

/**
 * 设置提示信息
 */
-(void)setHintsText:(NSString*)hints;
/**
 * 点击确定按钮
 */
-(IBAction)actionEnter:(id)sender;
@property (nonatomic, assign) iddelegate;
@end

@protocol WJDatePickerDelegate 
/**
 * 代理  点击确定后的事件
 */
@optional
-(void)DatePickerDelegateEnterActionWithDataPicker:(WJDatePicker*)picker;
@end

.m实现

#import "WJDatePicker.h"
#import "NSDate+FSExtension.h"

typedef enum {
    ePickerViewTagYear = 2016,
    ePickerViewTagMonth,
    ePickerViewTagDay,
    ePickerViewTagHour,
    ePickerViewTagMinute,
    ePickerViewTagSecond
}PickViewTag;

@interface WJDatePicker () 
@property (nonatomic, retain) UIPickerView* yearPicker;     // 年
@property (nonatomic, retain) UIPickerView* monthPicker;    // 月
@property (nonatomic, retain) UIPickerView* dayPicker;      // 日
@property (nonatomic, retain) UIPickerView* hourPicker;     // 时
@property (nonatomic, retain) UIPickerView* minutePicker;   // 分
@property (nonatomic, retain) UIPickerView* secondPicker;

@property (nonatomic, retain) UIToolbar*    toolBar;        // 工具条
@property (nonatomic, retain) UILabel*      hintsLabel;     // 提示信息

// dataSource
@property (nonatomic, retain) NSMutableArray* yearArray;
@property (nonatomic, retain) NSMutableArray* monthArray;
@property (nonatomic, retain) NSMutableArray* dayArray;
@property (nonatomic, retain) NSMutableArray* hourArray;
@property (nonatomic, retain) NSMutableArray* minuteArray;
@property (nonatomic, retain) NSMutableArray* secondArray;

@property (nonatomic, assign) NSUInteger yearValue;
@property (nonatomic, assign) NSUInteger monthValue;
@property (nonatomic, assign) NSUInteger dayValue;
@property (nonatomic, assign) NSUInteger hourValue;
@property (nonatomic, assign) NSUInteger minuteValue;
@property (nonatomic, assign) NSUInteger secondValue;

/**
 * 创建数据源
 */
-(void)createDataSource;

/**
 * create month Arrays
 */
-(void)createMonthArrayWithYear:(NSInteger)yearInt month:(NSInteger)monthInt;
@end

@implementation WJDatePicker

@synthesize delegate;
@synthesize date;

#pragma mark -
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, 0, 440, 260)];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor whiteColor]];
        self.yearArray = [[NSMutableArray alloc]initWithCapacity:0];
        self.monthArray = [[NSMutableArray alloc]initWithCapacity:0];
        self.dayArray = [[NSMutableArray alloc]initWithCapacity:0];
        self.hourArray = [[NSMutableArray alloc]initWithCapacity:0];
        self.minuteArray = [[NSMutableArray alloc]initWithCapacity:0];
        self.secondArray = [[NSMutableArray alloc]initWithCapacity:0];
        
        // 更新数据源
        [self createDataSource];
        
        // 创建 toolBar & hintsLabel & enter button
        self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
        [self.toolBar setBackgroundColor:[UIColor blueColor]];
        [self addSubview:self.toolBar];
        
//        UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        cancleBtn.frame = CGRectMake(0, 5, (self.frame.size.width-200)/2, 34);
//        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
//        [cancleBtn addTarget:self action:@selector(actionCancle:) forControlEvents:UIControlEventTouchUpInside];
//        [self.toolBar addSubview:cancleBtn];
        
        self.hintsLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.frame.size.width-200)/2, 5, 200, 34)];
        self.hintsLabel.textAlignment = NSTextAlignmentCenter;
        [self.hintsLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [self.hintsLabel setTextColor:[UIColor whiteColor]];
        [self.toolBar addSubview:self.hintsLabel];
        
        UIButton* tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [tempBtn setTitle:@"确定" forState:UIControlStateNormal];
        tempBtn.frame = CGRectMake(CGRectGetMaxX(_hintsLabel.frame), 5, (self.frame.size.width-200)/2, 34);
        [tempBtn addTarget:self action:@selector(actionEnter:) forControlEvents:UIControlEventTouchUpInside];
        [self.toolBar addSubview:tempBtn];
        
        NSArray *titles = @[@"月",@"日",@"时",@"分"];
        // 初始化各个视图
        self.yearPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 80, 216)];
        self.monthPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(81, 44, 60, 216)];
        self.dayPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(156, 44, 60, 216)];
        self.hourPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(231, 44, 60, 216)];
        self.minutePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(306, 44, 60, 216)];
        self.secondPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(381, 44, 60, 216)];
        
        for (NSInteger i=0; i endDate) {
        self.dayValue = endDate;
    }
    // 日
    [self.dayArray removeAllObjects];
    for(int i=1; i<=endDate; i++){
        [self.dayArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
}
#pragma mark - 点击确定按钮
-(IBAction)actionEnter:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(DatePickerDelegateEnterActionWithDataPicker:)]) {
        [self.delegate DatePickerDelegateEnterActionWithDataPicker:self];
    }
    [self removeFromSuperview];
}

//- (void)actionCancle:(UIButton *)btn{
//   [self removeFromSuperview];
//}
#pragma mark - 设置提示信息
-(void)setHintsText:(NSString*)hints{
    [self.hintsLabel setText:hints];
}
#pragma mark -
-(void)removeFromSuperview{
    self.delegate = nil;
    [super removeFromSuperview];
}
@end

你可能感兴趣的:(ios 自定义时间选择器)