9种场景全覆盖:iOS日期选择器WSDatePickerView完全指南

9种场景全覆盖:iOS日期选择器WSDatePickerView完全指南

【免费下载链接】DatePicker 日期选择器,日期时间选择,时间选择器 💯 【免费下载链接】DatePicker 项目地址: https://gitcode.com/gh_mirrors/date/DatePicker

你还在为iOS应用中的日期选择功能编写重复代码吗?还在为适配不同日期格式需求而头疼吗?本文将带你全面掌握WSDatePickerView——一款轻量级、高度可定制的iOS日期选择组件,帮你一站式解决各类日期选择场景。

读完本文你将获得:

  • 9种预设日期选择模式的应用场景与实现代码
  • 组件的完整集成流程(CocoaPods与手动集成)
  • 高级定制技巧:从颜色到日期范围的全维度配置
  • 性能优化与最佳实践指南

组件概述

WSDatePickerView是一个专为iOS平台设计的日期选择器(Date Picker)组件,采用Objective-C编写,支持ARC(Automatic Reference Counting,自动引用计数)机制。该组件提供了丰富的日期展示格式和高度的自定义能力,能够满足从简单的时间选择到复杂的日期范围限制等多种业务需求。

核心优势

特性说明
多模式支持提供9种预设日期格式,覆盖年月日时分到单独年份选择
高度定制支持按钮颜色、文本颜色、日期范围等10+项自定义属性
轻量级核心代码仅8个文件,无第三方依赖
易用性简洁API设计,3行代码即可实现基础日期选择功能
兼容性支持iOS全版本,自适应不同屏幕尺寸

组件结构

mermaid

快速集成

CocoaPods集成

WSDatePickerView提供CocoaPods支持,只需在你的Podfile中添加以下代码:

pod 'WSDatePickerView', '~> 1.0'

然后在终端执行:

pod install

手动集成

  1. 克隆仓库到本地:
git clone https://gitcode.com/gh_mirrors/date/DatePicker.git
  1. 将WSDatePickerView目录下的以下文件添加到你的Xcode项目中:

    • WSDatePickerView.h
    • WSDatePickerView.m
    • WSDatePickerView.xib
    • NSDate+Extension.h
    • NSDate+Extension.m
    • UIView+Extension.h
    • UIView+Extension.m
  2. 确保勾选"Copy items if needed"选项,并选择对应的target。

日期选择模式详解

WSDatePickerView提供9种预设日期选择模式,通过WSDateStyle枚举定义:

typedef enum{
    DateStyleShowYearMonthDayHourMinute  = 0, // 年月日时分
    DateStyleShowMonthDayHourMinute,        // 月日时分
    DateStyleShowYearMonthDay,              // 年月日
    DateStyleShowYearMonth,                 // 年月
    DateStyleShowMonthDay,                  // 月日
    DateStyleShowHourMinute,                // 时分
    DateStyleShowYear,                      // 年
    DateStyleShowMonth,                     // 月
    DateStyleShowDayHourMinute              // 日时分
}WSDateStyle;

1. 年月日时分选择(DateStyleShowYearMonthDayHourMinute)

这是最完整的日期时间选择模式,适用于需要精确到分钟的场景,如日程安排、预约系统等。

WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDayHourMinute CompleteBlock:^(NSDate *selectDate) {
    NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd HH:mm"];
    NSLog(@"选择的日期:%@", date);
    // 在这里处理选中的日期,如更新UI显示或保存到数据库
}];

// 自定义外观
datepicker.dateLabelColor = [UIColor orangeColor];      // 标题文字颜色
datepicker.datePickerColor = [UIColor blackColor];      // 滚轮文字颜色
datepicker.doneButtonColor = [UIColor orangeColor];     // 确定按钮颜色

// 显示选择器
[datepicker show];

2. 月日时分选择(DateStyleShowMonthDayHourMinute)

适用于不需要年份的重复事件,如生日提醒(每年重复)、周期性活动等。

WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowMonthDayHourMinute CompleteBlock:^(NSDate *selectDate) {
    NSString *date = [selectDate stringWithFormat:@"MM-dd HH:mm"];
    NSLog(@"选择的月日时分:%@", date);
}];
[datepicker show];

3. 年月日选择(DateStyleShowYearMonthDay)

最常用的日期选择模式,适用于生日、纪念日等日期选择场景。

WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay CompleteBlock:^(NSDate *selectDate) {
    NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd"];
    NSLog(@"选择的年月日:%@", date);
}];
[datepicker show];

4. 年月选择(DateStyleShowYearMonth)

适用于需要选择月份的场景,如信用卡有效期、月度报表筛选等。

WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonth CompleteBlock:^(NSDate *selectDate) {
    NSString *date = [selectDate stringWithFormat:@"yyyy-MM"];
    NSLog(@"选择的年月:%@", date);
}];
[datepicker show];

5. 月日选择(DateStyleShowMonthDay)

适用于不需要年份的日期选择,如节日、纪念日提醒等。

WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowMonthDay CompleteBlock:^(NSDate *selectDate) {
    NSString *date = [selectDate stringWithFormat:@"MM-dd"];
    NSLog(@"选择的月日:%@", date);
}];
[datepicker show];

6. 时分选择(DateStyleShowHourMinute)

适用于仅需要选择时间的场景,如闹钟设置、预约时间段等。

WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowHourMinute CompleteBlock:^(NSDate *selectDate) {
    NSString *date = [selectDate stringWithFormat:@"HH:mm"];
    NSLog(@"选择的时分:%@", date);
}];
[datepicker show];

7-9. 单单位选择模式

分别适用于仅需要选择年、月或日时分的特殊场景:

// 年份选择
WSDatePickerView *yearPicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYear CompleteBlock:^(NSDate *selectDate) {
    NSString *year = [selectDate stringWithFormat:@"yyyy"];
    NSLog(@"选择的年份:%@", year);
}];
[yearPicker show];

// 月份选择
WSDatePickerView *monthPicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowMonth CompleteBlock:^(NSDate *selectDate) {
    NSString *month = [selectDate stringWithFormat:@"MM"];
    NSLog(@"选择的月份:%@", month);
}];
[monthPicker show];

// 日时分选择
WSDatePickerView *dayTimePicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowDayHourMinute CompleteBlock:^(NSDate *selectDate) {
    NSString *dayTime = [selectDate stringWithFormat:@"dd HH:mm"];
    NSLog(@"选择的日时分:%@", dayTime);
}];
[dayTimePicker show];

高级配置

日期范围限制

通过设置maxLimitDateminLimitDate属性,可以限制用户选择的日期范围,适用于如酒店预订(只能选择未来日期)、历史数据查询(只能选择过去日期)等场景。

// 创建日期格式化器
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];

// 设置日期范围:今天到未来30天
NSDate *minDate = [NSDate date]; // 当前日期
NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:30*24*60*60]; // 30天后

WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDayHourMinute CompleteBlock:^(NSDate *selectDate) {
    NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd HH:mm"];
    NSLog(@"选择的日期:%@", date);
}];

// 设置日期范围限制
datepicker.minLimitDate = minDate;
datepicker.maxLimitDate = maxDate;

[datepicker show];

初始日期设置

除了默认滚动到当前日期外,还可以通过initWithDateStyle:scrollToDate:CompleteBlock:方法指定初始滚动日期:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *initialDate = [formatter dateFromString:@"2023-12-25 08:30"];

WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDayHourMinute scrollToDate:initialDate CompleteBlock:^(NSDate *selectDate) {
    NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd HH:mm"];
    NSLog(@"选择的日期:%@", date);
}];

// 隐藏大号年份标签
datepicker.yearLabelColor = [UIColor clearColor];

[datepicker show];

完整自定义属性

WSDatePickerView提供了丰富的自定义属性,可实现与App整体风格的完美融合:

// 宏定义,方便颜色设置
#define RGB(x,y,z) [UIColor colorWithRed:x/255.0 green:y/255.0 blue:z/255.0 alpha:1.0]

WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay CompleteBlock:^(NSDate *selectDate) {
    // 处理选择结果
}];

// 自定义颜色
datepicker.doneButtonColor = RGB(65, 188, 241);  // 确定按钮颜色
datepicker.dateLabelColor = RGB(255, 102, 0);    // 标题文字颜色
datepicker.datePickerColor = [UIColor darkGrayColor]; // 滚轮文字颜色
datepicker.yearLabelColor = RGB(200, 200, 200);  // 年份标签颜色

// 自定义日期范围
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
NSDate *minDate = [formatter dateFromString:@"2000"];
NSDate *maxDate = [formatter dateFromString:@"2030"];
datepicker.minLimitDate = minDate;
datepicker.maxLimitDate = maxDate;

[datepicker show];

实战案例

案例1:酒店预订日期选择

// 酒店预订只能选择今天及以后,且入住和离店日期需间隔至少1天
- (void)showCheckInDatePicker {
    NSDate *minDate = [NSDate date]; // 今天
    NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:90*24*60*60]; // 90天后
    
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay scrollToDate:minDate CompleteBlock:^(NSDate *selectDate) {
        self.checkInDate = selectDate;
        self.checkInLabel.text = [selectDate stringWithFormat:@"yyyy-MM-dd"];
        
        // 自动弹出离店日期选择器
        [self showCheckOutDatePicker];
    }];
    
    datepicker.minLimitDate = minDate;
    datepicker.maxLimitDate = maxDate;
    datepicker.doneButtonColor = RGB(52, 152, 219);
    datepicker.dateLabelColor = RGB(52, 152, 219);
    
    [datepicker show];
}

- (void)showCheckOutDatePicker {
    // 离店日期必须晚于入住日期至少1天
    NSDate *minDate = [self.checkInDate dateByAddingTimeInterval:24*60*60];
    NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:90*24*60*60];
    
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay scrollToDate:minDate CompleteBlock:^(NSDate *selectDate) {
        self.checkOutDate = selectDate;
        self.checkOutLabel.text = [selectDate stringWithFormat:@"yyyy-MM-dd"];
        
        // 计算并显示价格
        [self calculatePrice];
    }];
    
    datepicker.minLimitDate = minDate;
    datepicker.maxLimitDate = maxDate;
    datepicker.doneButtonColor = RGB(52, 152, 219);
    datepicker.dateLabelColor = RGB(52, 152, 219);
    
    [datepicker show];
}

案例2:生日选择器

- (void)showBirthdayPicker {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    // 生日选择范围:1900年到15年前
    NSDate *minDate = [formatter dateFromString:@"1900-01-01"];
    NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:-15*365*24*60*60];
    
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay CompleteBlock:^(NSDate *selectDate) {
        self.birthdayLabel.text = [selectDate stringWithFormat:@"yyyy-MM-dd"];
        
        // 计算年龄
        NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear 
                                                                       fromDate:selectDate 
                                                                         toDate:[NSDate date] 
                                                                        options:0];
        self.ageLabel.text = [NSString stringWithFormat:@"%ld岁", (long)components.year];
    }];
    
    datepicker.minLimitDate = minDate;
    datepicker.maxLimitDate = maxDate;
    datepicker.dateLabelColor = [UIColor brownColor];
    datepicker.doneButtonColor = [UIColor brownColor];
    
    [datepicker show];
}

性能优化

内存管理

WSDatePickerView采用ARC机制,使用完后会自动释放内存。为避免内存泄漏,需注意以下几点:

  1. 不要在block中强引用self,应使用weak self:
__weak typeof(self) weakSelf = self;
WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay CompleteBlock:^(NSDate *selectDate) {
    __strong typeof(weakSelf) strongSelf = weakSelf;
    if (strongSelf) {
        strongSelf.selectedDate = selectDate;
        [strongSelf updateUI];
    }
}];
  1. 不需要时及时移除:虽然组件设计为显示后自动处理生命周期,但在视图控制器销毁时,最好确保没有未完成的日期选择器。

性能优化建议

  1. 避免频繁创建实例:对于需要多次使用的场景,可考虑创建一个单例实例重复使用。

  2. 合理设置日期范围:限制日期范围可以减少滚轮数据量,提高滚动流畅度。

  3. 避免在block中执行复杂操作:选择日期后的处理逻辑应尽量简洁,复杂操作应异步执行。

常见问题解答

Q1: 如何隐藏年份大标题?

A1: 通过设置yearLabelColor属性为透明色即可隐藏年份大标题:

datepicker.yearLabelColor = [UIColor clearColor];

Q2: 如何自定义日期格式?

A2: WSDatePickerView使用系统NSDateFormatter,支持所有标准日期格式:

// 支持的格式示例
[selectDate stringWithFormat:@"yyyy年MM月dd日 HH时mm分"]; // 中文格式
[selectDate stringWithFormat:@"MM/dd/yyyy"]; // 美式格式
[selectDate stringWithFormat:@"dd-MM-yyyy"]; // 欧式格式

Q3: 如何处理时区问题?

A3: WSDatePickerView默认使用设备本地时区,如需处理特定时区,可在格式化日期时设置时区:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"]; // 设置为GMT时区
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *dateString = [formatter stringFromDate:selectDate];

Q4: 如何在Swift项目中使用?

A4: WSDatePickerView虽然是Objective-C编写,但完全可以在Swift项目中使用:

  1. 创建桥接文件(Bridging-Header.h)
  2. 添加#import "WSDatePickerView.h"
  3. 在Swift中使用:
let datepicker = WSDatePickerView(dateStyle: DateStyleShowYearMonthDay) { (selectDate) in
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    let dateString = formatter.string(from: selectDate as Date)
    print("选择的日期:\(dateString)")
}
datepicker?.show()

总结与展望

WSDatePickerView凭借其丰富的功能、高度的可定制性和简洁的API设计,为iOS开发者提供了一个高效的日期选择解决方案。无论是简单的时间选择还是复杂的日期范围限制,都能通过几行代码快速实现。

未来版本可能会增加的功能:

  • 支持更多日期格式和自定义显示
  • 暗黑模式(Dark Mode)适配
  • 时间间隔选择(如每15分钟、每小时)
  • Swift版本实现

如果你在使用过程中遇到任何问题或有功能建议,欢迎通过项目仓库提交issue或Pull Request,让我们共同完善这个实用的日期选择组件。

最后,如果你觉得WSDatePickerView对你的项目有帮助,请不要吝啬你的Star,这将是对开发者最大的鼓励!

【免费下载链接】DatePicker 日期选择器,日期时间选择,时间选择器 💯 【免费下载链接】DatePicker 项目地址: https://gitcode.com/gh_mirrors/date/DatePicker

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值