iOS 日历定制

#import

 

NS_ASSUME_NONNULL_BEGIN

 

@interface TimeDateModel : NSObject

@property (nonatomic , strong)NSString *year;

@property (nonatomic , strong)NSString *mouth;

@property (nonatomic , strong)NSString *day;

@property (nonatomic , assign)CGFloat timeStamp;

@property (nonatomic , strong)NSMutableArray *moutharray;

@property (nonatomic , strong)NSMutableArray *dayarray;

 

@end

 

@interface CX_ChooseTimeVC : UIViewController

@property (nonatomic , assign)CGFloat starTime;

@property (nonatomic , assign)CGFloat endTime;

 

@end

 

NS_ASSUME_NONNULL_END

 

#import "CX_ChooseTimeVC.h"

@interface TimeDateModel ()

 

@end

 

@implementation TimeDateModel

 

- (instancetype)init{

    self = [super init];

    if (self) {

        self.moutharray = [NSMutableArray array];

        self.dayarray = [NSMutableArray array];

 

    }

    return self;

}

@end

 

 

@interface CX_ChooseTimeVC ()

//datasource

@property (nonatomic , strong)NSMutableArray  *allModelArr;

@property (nonatomic , strong)NSMutableArray *yearArr;

@property (nonatomic , strong)NSMutableArray  *mouthArr;

@property (nonatomic , strong)NSMutableArray  *dayArr;

@property (nonatomic , strong)TimeDateModel *currentChoose;

 

//view

@property (nonatomic , strong)UIView *bkView;

@property (nonatomic , strong)UIButton *disBtn;

@property (nonatomic , strong)UILabel *titleLabel;

@property (nonatomic , strong)UIButton *sureBtn;

@property (nonatomic , strong)UILabel *lineLabel;

@property (nonatomic , strong)UIButton *fBtn;

@property (nonatomic , strong)UIButton *sBtn;

@property (nonatomic , strong)UIButton *tBtn;

@property (nonatomic , strong)UIPickerView *pickerView;

 

 

 

@end

 

@implementation CX_ChooseTimeVC

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    [self getAllModelArr];

    [self getYearUseModelArr];

    [self getMouthUseModelArr];

    [self getDayUseModelArr];

    

    [self.view addSubview:self.bkView];

    [self.bkView addSubview:self.disBtn];

    [self.bkView addSubview:self.sureBtn];

    [self.bkView addSubview:self.lineLabel];

    [self.bkView addSubview:self.fBtn];

    [self.bkView addSubview:self.sBtn];

    [self.bkView addSubview:self.tBtn];

    [self.bkView addSubview:self.pickerView];

}

 

 

//获取全部时间

- (void)getAllModelArr{

    CGFloat startTime = self.starTime;

    while (startTime <= self.endTime) {

        NSString *timeStr = [CX_Tool convertStrToTime:[NSString stringWithFormat:@"%f",startTime]];

        NSArray *timeArr = [timeStr componentsSeparatedByString:@"/"];

        TimeDateModel *timeDateModel = [[TimeDateModel alloc]init];

        timeDateModel.year = timeArr[0];

        timeDateModel.mouth = timeArr[1];

        timeDateModel.day = [timeArr lastObject];

        timeDateModel.timeStamp = startTime;

        startTime = startTime + 24 * 60 * 60;

        [self.allModelArr addObject:timeDateModel];

    }

    self.currentChoose = [self.allModelArr firstObject];

}

//年份

- (void)getYearUseModelArr{

    for (int i = 0; i < self.allModelArr.count; i++) {

        TimeDateModel *timeDateModel = [self.allModelArr objectAtIndex:i];

        BOOL is = [self isHasYear:timeDateModel];

        if (is == NO) {

            [self.yearArr addObject:timeDateModel];

        }

    }

}

 

- (BOOL)isHasYear:(TimeDateModel *)timeDateModel{

    BOOL is = NO;

    for (TimeDateModel *model in self.yearArr) {

        if ([model.year isEqualToString:timeDateModel.year] == YES) { //Yes表示有重复的年份

            is = YES;

            break;

        }

    }

    return is;

}

 

//月份

- (void)getMouthUseModelArr{

    for (int i = 0; i < self.allModelArr.count; i++) {

        TimeDateModel *timeDateModel = [self.allModelArr objectAtIndex:i];

        TimeDateModel * t = [self getYearModelUseTimeDateModel:timeDateModel];

        BOOL is = [self isHasYearMouth:timeDateModel andAddT:t];

        if (is == NO) {

            [t.moutharray addObject:timeDateModel];

        }

    }

}

 

- (TimeDateModel *)getYearModelUseTimeDateModel:(TimeDateModel *)timeDateModel{

    

    TimeDateModel *t;

    for (TimeDateModel *model in self.yearArr) {

        if ([model.year isEqualToString:timeDateModel.year] == YES) {

            t = model;

            break;

        }

    }

    return t;

}

 

- (BOOL)isHasYearMouth:(TimeDateModel *)timeDateModel andAddT:(TimeDateModel *)t{

    BOOL is = NO;

    for (TimeDateModel  *model in t.moutharray) {

        if ([model.mouth isEqualToString:timeDateModel.mouth]) {

            is = YES;

            break;

        }

    }

    return is;

}

//日

- (void)getDayUseModelArr{

    for (int i = 0; i < self.allModelArr.count; i++) {

        TimeDateModel *timeDateModel = [self.allModelArr objectAtIndex:i];

        TimeDateModel * t = [self getYearModelUseTimeDateModel:timeDateModel];

        TimeDateModel * m = [self getYearModelUseTimeDateModel:t andCurrent:timeDateModel];

        BOOL is = [self isHasMouthDay:timeDateModel andAddM:m];

        if (is == NO) {

            [m.dayarray addObject:timeDateModel];

        }

    }

}

 

- (TimeDateModel *)getYearModelUseTimeDateModel:(TimeDateModel *)yearmodel andCurrent:(TimeDateModel *)currentModel{

    

    TimeDateModel *mouthModel;

    for (TimeDateModel *model in yearmodel.moutharray) {

        if ([model.mouth isEqualToString:currentModel.mouth] == YES) {

            mouthModel = model;

            break;

        }

    }

    return mouthModel;

}

 

- (BOOL)isHasMouthDay:(TimeDateModel *)timeDateModel andAddM:(TimeDateModel *)m{

    BOOL is = NO;

    for (TimeDateModel  *model in m.dayarray) {

        if ([model.day isEqualToString:timeDateModel.day]) {

            is = YES;

            break;

        }

    }

    return is;

}

 

- (NSMutableArray *)allModelArr{

    if (_allModelArr == nil) {

        _allModelArr = [NSMutableArray array];

    }

    return _allModelArr;

}

 

- (NSMutableArray *)yearArr{

    if (_yearArr == nil) {

        _yearArr = [NSMutableArray array];

    }

    return _yearArr;

}

 

- (NSMutableArray *)mouthArr{

    if (_mouthArr == nil) {

        _mouthArr = [NSMutableArray array];

    }

    return _mouthArr;

}

 

- (NSMutableArray *)dayArr{

    if (_dayArr == nil) {

        _dayArr = [NSMutableArray array];

    }

    return _dayArr;

}

 

 

- (UIView *)bkView{

    if (_bkView == nil) {

        _bkView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREENH_HEIGHT_CX - ThisScreecHeight(260), SCREEN_WIDTH_CX, ThisScreecHeight(260))];

        _bkView.backgroundColor = [UIColor whiteColor];

        

    }

    return _bkView;

}

 

- (UILabel *)titleLabel{

    if (_titleLabel == nil) {

        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.disBtn.frame), 0, SCREEN_WIDTH_CX - 2 * ThisScreecWidth(60), ThisScreecHeight(50))];

        _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14];

        _titleLabel.textAlignment = NSTextAlignmentCenter;

        _titleLabel.text = @"";

    }

    return _titleLabel;

}

 

- (UIButton *)disBtn{

    if (_disBtn == nil) {

        _disBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [_disBtn setTitle:@"取消" forState:UIControlStateNormal];

        _disBtn.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14];

        [_disBtn addTarget:self action:@selector(disBtnAction:) forControlEvents:UIControlEventTouchUpInside];

        _disBtn.frame = CGRectMake(0, 0, ThisScreecWidth(60), ThisScreecHeight(50));

        [_disBtn setTitleColor:RGBCOLOR(51, 51, 51) forState:UIControlStateNormal];

    }

    return _disBtn;

}

 

- (UIButton *)sureBtn

{

    if (_sureBtn == nil) {

        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];

        _sureBtn.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14];

        [_sureBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];

        _sureBtn.frame = CGRectMake(SCREEN_WIDTH_CX - ThisScreecWidth(60), 0, ThisScreecWidth(60), ThisScreecHeight(50));

        [_sureBtn setTitleColor:RGBCOLOR(254, 137, 79) forState:UIControlStateNormal];

        

    }

    return _sureBtn;

}

- (UILabel *)lineLabel{

    if (_lineLabel == nil) {

        _lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.sureBtn.frame), SCREEN_WIDTH_CX - 20, 1)];

        _lineLabel.backgroundColor = RGBCOLOR(230, 230, 230);

    }

    return _lineLabel;

}

 

- (void)disBtnAction:(UIButton *)sender{

    [self dismissViewControllerAnimated:NO completion:nil];

}

 

- (void)sureBtnAction:(UIButton *)sender{

//    NSLog(@"选择~~%@",self.carTitle);

//    if (self.chooseBtnBlock) {

//        self.chooseBtnBlock(1,self.carTitle);

//    }

//    if (self.chooseBtnBlock3) {

//        self.chooseBtnBlock3(1,self.carTitle,self.curColor);

//    }

    

    NSLog(@"%@年%@月%@日 时间戳%f",self.currentChoose.year,self.currentChoose.mouth,self.currentChoose.day,self.currentChoose.timeStamp);

    

//    [self dismissViewControllerAnimated:NO completion:nil];

}

 

- (UIButton *)fBtn{

    if (_fBtn == nil) {

        _fBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [_fBtn setTitle:@"年" forState:UIControlStateNormal];

        _fBtn.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14];

        [_fBtn addTarget:self action:@selector(disBtnAction:) forControlEvents:UIControlEventTouchUpInside];

        _fBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineLabel.frame), SCREEN_WIDTH_CX/3, ThisScreecHeight(30));

        [_fBtn setTitleColor:RGBCOLOR(51, 51, 51) forState:UIControlStateNormal];

    }

    return _fBtn;

}

 

- (UIButton *)sBtn{

    if (_sBtn == nil) {

        _sBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [_sBtn setTitle:@"月" forState:UIControlStateNormal];

        _sBtn.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14];

        [_sBtn addTarget:self action:@selector(disBtnAction:) forControlEvents:UIControlEventTouchUpInside];

        _sBtn.frame = CGRectMake(SCREEN_WIDTH_CX/3, CGRectGetMaxY(self.lineLabel.frame), SCREEN_WIDTH_CX/3, ThisScreecHeight(30));

        [_sBtn setTitleColor:RGBCOLOR(51, 51, 51) forState:UIControlStateNormal];

    }

    return _sBtn;

}

 

- (UIButton *)tBtn{

    if (_tBtn == nil) {

        _tBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [_tBtn setTitle:@"日" forState:UIControlStateNormal];

        _tBtn.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14];

        [_tBtn addTarget:self action:@selector(disBtnAction:) forControlEvents:UIControlEventTouchUpInside];

        _tBtn.frame = CGRectMake(SCREEN_WIDTH_CX*2/3, CGRectGetMaxY(self.lineLabel.frame), SCREEN_WIDTH_CX/3, ThisScreecHeight(30));

        [_tBtn setTitleColor:RGBCOLOR(51, 51, 51) forState:UIControlStateNormal];

    }

    return _tBtn;

}

 

- (UIPickerView *)pickerView{

    if (_pickerView == nil) {

        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.fBtn.frame), SCREEN_WIDTH_CX, CGRectGetHeight(self.bkView.frame) - CGRectGetMaxY(self.sureBtn.frame))];

        _pickerView.delegate = self;

        _pickerView.dataSource = self;

        _pickerView.showsSelectionIndicator = YES;

        

    }

    return _pickerView;

}

 

 

 

#pragma -mark pickerview

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 3;

}

 

//返回指定列的行数

 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    

    if (component == 0){

        return self.yearArr.count;

    }else if (component == 1){

        if (self.mouthArr.count <=0) {

            TimeDateModel *timeDateModel = [self.yearArr objectAtIndex:0];

            [self.mouthArr addObjectsFromArray:timeDateModel.moutharray];

        }

        

        return self.mouthArr.count;

    }else if (component == 2){

        if (self.dayArr.count <=0) {

            TimeDateModel *timeDateModel = [self.mouthArr objectAtIndex:0];

            [self.dayArr addObjectsFromArray:timeDateModel.dayarray];

        }

        

        return self.dayArr.count;

    }

    return 0;

}

 

//返回指定列,行的高度,就是自定义行的高度

 

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

    return 40.0f;

}

 

//返回指定列的宽度

 

- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

{

    return  self.pickerView.frame.size.width/3;

}

 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if (component == 0) {

        TimeDateModel *timeDateModel = [self.yearArr objectAtIndex:row];

        [self.mouthArr removeAllObjects];

        [self.mouthArr addObjectsFromArray:timeDateModel.moutharray];

        

        TimeDateModel *daytimeDateModel = [self.mouthArr objectAtIndex:0];

        [self.dayArr removeAllObjects];

        [self.dayArr addObjectsFromArray:daytimeDateModel.dayarray];

        

        [pickerView selectedRowInComponent:1];

        [pickerView reloadComponent:1];

        [pickerView selectRow:0 inComponent:1 animated:YES];

        

        [pickerView selectedRowInComponent:2];

        [pickerView reloadComponent:2];

        [pickerView selectRow:0 inComponent:2 animated:YES];

 

        self.currentChoose = [self.dayArr objectAtIndex:0];

 

    }else if (component == 1){

        TimeDateModel *timeDateModel = [self.mouthArr objectAtIndex:row];

        [self.dayArr removeAllObjects];

        [self.dayArr addObjectsFromArray:timeDateModel.dayarray];

 

        [pickerView selectedRowInComponent:2];

        [pickerView reloadComponent:2];

        [pickerView selectRow:0 inComponent:2 animated:YES];

        

        self.currentChoose = [self.dayArr objectAtIndex:0];

 

    }else if (component == 2){

        [pickerView reloadComponent:2];

        

        self.currentChoose = [self.dayArr objectAtIndex:row];

    }

}

 

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

    

    if (!view){

        

        view = [[UIView alloc] init];

        

    }

    NSString *text = @"";

    if(component == 0)

    {

        text = [self.yearArr objectAtIndex:row].year;

    }

    else if(component == 1)

    {

        text = [self.mouthArr objectAtIndex:row].mouth;

    }

    else if(component == 2){

        text = [self.dayArr objectAtIndex:row].day;

    }

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_CX/3, 40)];

    label.textColor = MX_TITLE_FONT_COLOR;

    label.textAlignment = NSTextAlignmentCenter;

    label.text = text;

    [view addSubview:label];

    return view;

}

@end

 

 

 

//时间戳变为格式时间

+ (NSString *)convertStrToTime:(NSString *)timeStr{

    long long time=[timeStr longLongValue];

    //    如果服务器返回的是13位字符串,需要除以1000,否则显示不正确(13位其实代表的是毫秒,需要除以1000)

    //    long long time=[timeStr longLongValue] / 1000;

    

    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];

    

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

    

    [formatter setDateFormat:@"yyyy/MM/dd"];

    

    NSString*timeString=[formatter stringFromDate:date];

    

    return timeString;

    

}

你可能感兴趣的:(iOS知识点)