美文网首页
iOS 自定义时间选择器

iOS 自定义时间选择器

作者: KingTortoise | 来源:发表于2017-08-31 14:54 被阅读0次

前序

在阅读公司项目代码的时候发现使用到了五级的时间选择器,当时是使用一个UIDatePicker和一个两级的UIPickerView来实现的,但是在样式上还有点丑陋,所以我自己使用UIPickerView封装了一个五级的时间选择器。

效果展示

默认效果.gif 手动设置时间.gif 不可选择小于手动设置的时间.gif 设置最大最小时间.gif 手动设置时间和最大最小时间.gif

相关属性

@interface PCDatePickerView : UIView
/**
 * @property maximumDate: 最大值
 */
@property (nonatomic, strong) NSDate *maximumDate;
/**
 * @property minimumDate: 最小值
 */
@property (nonatomic, strong) NSDate *minimumDate;
/**
 * @property date: 用于手动设置时间
 */
@property (nonatomic, strong) NSDate *date;
/**
 * @property doneAction: 用于回调
 */
@property (nonatomic, copy) ToolBarAction doneAction;
/**
 * @property canChoicePastTime: 是否可以选择过去的时间 默认是NO(不可选)
 */
@property (nonatomic, assign) BOOL canChoicePastTime;
/**
 * @property backGroundColor: 选择器背景颜色
 */
@property (nonatomic, strong) UIColor *backGroundColor;

/**
 * 隐藏视图
 */
- (void)hide;

/**
 * 显示视图
 */
- (void)showView;
@end

使用实例

@interface DPTestViewController ()
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UITextField *yearTextField;
@property (weak, nonatomic) IBOutlet UITextField *monthTextField;
@property (weak, nonatomic) IBOutlet UITextField *dayTextField;
@property (weak, nonatomic) IBOutlet UITextField *hourTextField;
@property (weak, nonatomic) IBOutlet UITextField *minuteTextField;
@property (weak, nonatomic) IBOutlet UIButton *isSelectedBtn;
@property (weak, nonatomic) IBOutlet UITextField *minTimeTextField;
@property (weak, nonatomic) IBOutlet UITextField *maxTimeTextField;


@property (assign, nonatomic) BOOL isSelected;

@property (nonatomic, strong) PCDatePickerView *datePicker;
@end

@implementation DPTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationBarBackButton];
    self.datePicker = [[PCDatePickerView alloc] init];
    [self.view addSubview:self.datePicker];
    __weak __typeof(&*self) weakSelf = self;
    _datePicker.doneAction = ^(NSDate *date){
        [weakSelf sureDatePickerView:date];
    };
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)sureDatePickerView:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *strDate = [dateFormatter stringFromDate:date];
    self.dateLabel.text = strDate;
}


- (IBAction)canSelectPastTimeAction:(id)sender {
    [self.datePicker hide];
    self.isSelected = !self.isSelected;
    NSString *str = self.isSelected ? @"可选择":@"不可选择";
    [self.isSelectedBtn setTitle:str forState:UIControlStateNormal];
}


- (IBAction)btnAction:(id)sender {
    NSString *year = self.yearTextField.text;
    NSString *month = self.monthTextField.text;
    NSString *day = self.dayTextField.text;
    NSString *hour = self.hourTextField.text;
    NSString *minute = self.minuteTextField.text;
    NSString *time = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",year,month,day,hour,minute];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *date = [dateFormatter dateFromString:time];
    self.datePicker.date =  date;
    NSString *minTime = self.minTimeTextField.text;
    NSDate *minDate = [dateFormatter dateFromString:minTime];
    self.datePicker.minimumDate = minDate;
    NSString *maxTime = self.maxTimeTextField.text;
    NSDate *maxDate = [dateFormatter dateFromString:maxTime];
    self.datePicker.maximumDate = maxDate;
    self.datePicker.canChoicePastTime = self.isSelected;
    [self.datePicker showView];
}
@end

结束语

该控件的使用还是比较的简单的,希望这篇文章给你有帮助。如果有需要的话,我会单独写一篇来分享我的思路。
欢迎来我的Github网址下载demo

相关文章

网友评论

      本文标题:iOS 自定义时间选择器

      本文链接:https://www.haomeiwen.com/subject/pueqjxtx.html