iOS 年月日选择器

2019-06-22  本文已影响0人  邓布利多教授

1、创建一个集成PickerView的View

.h文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface LYDateIntervalSelectorPicker : UIPickerView<UIPickerViewDelegate, UIPickerViewDataSource>

-(instancetype)initWithDatePickerView;

@property (nonatomic, strong, readonly) NSDate *date;

@end

NS_ASSUME_NONNULL_END

.m文件

#import "LYDateIntervalSelectorPicker.h"

static CGFloat rowsHeight = 44.0;

@interface LYDateIntervalSelectorPicker ()

@property (nonatomic, strong) NSIndexPath *todayIndexPath;
@property (nonatomic, strong) NSArray *months;
@property (nonatomic, strong) NSArray *years;
@property (nonatomic, strong) NSArray *days;
@property (nonatomic, strong) NSCalendar *calendar;

@end

@implementation LYDateIntervalSelectorPicker

-(instancetype)initWithDatePickerView{
    
    self = [super init];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        
        self.years = [self nameOfYears];
        self.months = [self nameOfMonths];
        self.days = [self nameOfDays];
        self.todayIndexPath = [self todayPath];
        [self selectCurrentDate];
        
    }
    return self;
    
}

- (void)selectCurrentDate{
    
    NSIndexPath *selectIndexPath = [self todayPath];
    
    //设置当前年份
    [self selectRow:selectIndexPath.section
        inComponent:0
           animated:YES];
    [self pickerView:self didSelectRow:selectIndexPath.row inComponent:0];
    
    selectIndexPath = [self todayPath];
    
    //设置当前月份
    [self selectRow:selectIndexPath.row
        inComponent:1
           animated:YES];
    [self pickerView:self didSelectRow:selectIndexPath.row inComponent:1];
    
    //设置当前日期
    CGFloat day = [[[self currentDayName] substringToIndex:[self currentDayName].length] floatValue] - 1;
    [self selectRow:day inComponent:2 animated:YES];
    [self pickerView:self didSelectRow:day inComponent:2];
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    //获取当前选中的是几号
    NSInteger currert = [pickerView selectedRowInComponent:2] + 1;
    
    //判断二月份最大天数和30号和31号
    if (currert > [self daysCountWithSelectDate]) {
        [pickerView selectRow:[self daysCountWithSelectDate] inComponent:2 animated:NO];
    }
    
    if (component == 0 || component == 1) {
        self.days = [self nameOfDays];
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
    }
    
}

-(NSDate *)date{

    NSString *year = [self.years objectAtIndex:([self selectedRowInComponent:0])];
    NSString *month = [self.months objectAtIndex:([self selectedRowInComponent:1])];
    NSString *day = [self.days objectAtIndex:([self selectedRowInComponent:2]) % self.days.count];

    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy年M月d日"];
    NSString *dateString = [NSString stringWithFormat:@"%@%@%@", year, month, day];
    NSDate *date = [formatter dateFromString:dateString];
    return date;

}

#pragma mark - UIPickerViewDelegate

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return [self componentWidth];
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel *returnView = [self labelForComponent:component];
    returnView.text = [self titleForRow:row forComponent:component];
    return returnView;
    
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return rowsHeight;
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if(component == 0){
        return self.years.count;
    }else if (component == 1) {
        return self.months.count;
    }else {
        return self.days.count;
    }
    
}

-(CGFloat)componentWidth{
    return self.bounds.size.width / 3;
}

-(NSString *)titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if(component == 0) {
        return [self.years objectAtIndex:(row)];
    }else if(component == 1) {
        return [self.months objectAtIndex:(row)];
    }else {
        NSInteger DayCount = self.days.count;
        return [self.days objectAtIndex:(row % DayCount)];
    }
    
}

-(UILabel *)labelForComponent:(NSInteger)component{
    
    CGRect frame = CGRectMake(0, 0, [self componentWidth], rowsHeight);
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.userInteractionEnabled = NO;
    return label;
    
}



#pragma mark --------- 华丽的分割线 ---------



//当前时间
-(NSIndexPath *)todayPath{
    
    CGFloat row = 0.f;
    CGFloat section = 0.f;
    
    NSString *year  = [self currentYearName];
    NSString *month = [self currentMonthName];
    
    for(NSString *cellYear in self.years) {
        
        if([cellYear isEqualToString:year]) {
            section = [self.years indexOfObject:cellYear];
            break;
        }
        
    }
    
    for(NSString *cellMonth in self.months) {
        
        if([cellMonth isEqualToString:month]) {
            row = [self.months indexOfObject:cellMonth];
            break;
        }
        
    }
    
    return [NSIndexPath indexPathForRow:row inSection:section];
    
}

//年份数组
-(NSArray *)nameOfYears{
    
    NSMutableArray *years = [NSMutableArray array];
    NSInteger currentYear = [[[self currentYearName] substringToIndex:4] integerValue];
    
    for(NSInteger year = currentYear - 5; year <= currentYear; year++) {
        NSString *yearStr = [NSString stringWithFormat:@"%li年", (long)year];
        [years addObject:yearStr];
    }
    return years;
    
}

//月份数组
-(NSArray *)nameOfMonths{
    return @[@"1月", @"2月", @"3月", @"4月", @"5月", @"6月", @"7月", @"8月", @"9月", @"10月", @"11月", @"12月"];
}

//日期数组
-(NSArray *)nameOfDays{
    
    NSUInteger numberOfDaysInMonth = [self daysCountWithSelectDate];
    NSMutableArray *tempArr = [NSMutableArray array];
    for (int i = 1; i < numberOfDaysInMonth + 1 ; i ++) {
        NSString *day = [NSString stringWithFormat:@"%d日",i];
        [tempArr addObject:day];
    }
    return tempArr;
    
}

//根据当前年月获取当前月天数
-(NSInteger)daysCountWithSelectDate{
    self.calendar = [NSCalendar currentCalendar];
    NSRange range = [self.calendar rangeOfUnit:NSCalendarUnitDay
                                        inUnit:NSCalendarUnitMonth
                                       forDate:[self monthDate]];
    return range.length;
}

//根据当前年月返回日期
-(NSDate *)monthDate{
    
    NSString *year = [self.years objectAtIndex:([self selectedRowInComponent:0])];
    NSString *month = [self.months objectAtIndex:([self selectedRowInComponent:1])];
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy年M月"];
    NSDate *date = [formatter dateFromString:[NSString stringWithFormat:@"%@%@", year, month]];
    return date;
    
}

//当前年份
-(NSString *)currentYearName{
    
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy年"];
    return [formatter stringFromDate:[NSDate date]];
    
}

//当前月份
-(NSString *)currentMonthName{
    
    NSDateFormatter *formatter = [NSDateFormatter new];
    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [formatter setLocale:usLocale];
    [formatter setDateFormat:@"M月"];
    return [formatter stringFromDate:[NSDate date]];
    
}

//当前日期
-(NSString *)currentDayName{
    
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"d日"];
    return [formatter stringFromDate:[NSDate date]];
    
}

@end

2、创建一个View

.h文件

#import <UIKit/UIKit.h>
#import "LYDateIntervalSelectorPicker.h"

NS_ASSUME_NONNULL_BEGIN

@interface LYDateIntervalSelectorView : UIView

//单例
+(LYDateIntervalSelectorView *)initClient;

-(void)datePickerCompleteBlock:(void (^)(NSDate *date))completeBlock;

@end

NS_ASSUME_NONNULL_END

.m文件 — 引入头文件
#import "LYDateIntervalSelectorPicker.h"

#import "LYDateIntervalSelectorView.h"
#import "LYDateIntervalSelectorPicker.h"

static CGFloat whiteViewHeight = 400.f;
static CGFloat pickerHeight = 250.f;

//时间回调
typedef void (^ DateBlock)(NSDate *);

@interface LYDateIntervalSelectorView ()
{
    CGFloat height;
    CGFloat width;
}

//白色背景
@property (nonatomic, strong) UIView *whiteView;

@property (nonatomic, copy) DateBlock dateBlock;

//选择器
@property (nonatomic, strong) LYDateIntervalSelectorPicker *selectorPicker;

@end

@implementation LYDateIntervalSelectorView

+(LYDateIntervalSelectorView *)initClient{
    
    static LYDateIntervalSelectorView *client = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        client = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [client createUI];
    });
    return client;
    
}

-(void)datePickerCompleteBlock:(void (^)(NSDate *date))completeBlock{
    
    _dateBlock = completeBlock;
    [self show];
    
}

#pragma mark - 创建布局
-(void)createUI{
    
    height = [UIScreen mainScreen].bounds.size.height;
    width = [UIScreen mainScreen].bounds.size.width;
    
    //取消手势
    UITapGestureRecognizer *cancelTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCancelAction)];
    [self addGestureRecognizer:cancelTap];
    
    //白色背景
    self.whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, height, width, whiteViewHeight)];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.whiteView];
    
    //完成
    UIButton *bConfirm = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, 40)];
    [bConfirm setTitle:@"完成" forState:0];
    [bConfirm setTitleColor:[UIColor cyanColor] forState:0];
    [bConfirm addTarget:self action:@selector(buttonConfirm) forControlEvents:UIControlEventTouchUpInside];
    [self.whiteView addSubview:bConfirm];
    
    //选择器
    self.selectorPicker = [[LYDateIntervalSelectorPicker alloc]initWithDatePickerView];
    self.selectorPicker.frame = CGRectMake(0, whiteViewHeight - pickerHeight, width, pickerHeight);
    [self.whiteView addSubview:self.selectorPicker];
    
    
}

-(void)buttonConfirm{
    
    if (_dateBlock) {
        _dateBlock(_selectorPicker.date);
    }
    
}

//显示手势
-(void)show{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.frame = CGRectMake(0, self->height - whiteViewHeight, self->width, whiteViewHeight);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
    
}

//取消手势
-(void)tapCancelAction{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.whiteView.frame = CGRectMake(0, self->height, self->width, whiteViewHeight);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

@end

3、创建一个Controller

.h文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface DateSelectorController : UIViewController

@end

NS_ASSUME_NONNULL_END

.m文件 —— 引入头文件
#import "LYDateIntervalSelectorView.h"

#import "DateSelectorController.h"
#import "LYDateIntervalSelectorView.h"

@interface DateSelectorController ()

@end

@implementation DateSelectorController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [[LYDateIntervalSelectorView initClient] datePickerCompleteBlock:^(NSDate * _Nonnull date) {
        
        NSString *formatStr = @"yyyy年MM月dd日";
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:formatStr];
        NSLog(@"%@",[dateFormatter stringFromDate:date]);
        
    }];
    
}

@end

4、效果图

效果图.gif

5、全剧终

上一篇下一篇

猜你喜欢

热点阅读