自定义一个日期选择器
自定义一个日期选择器
默默沫沫shine 发表于2年前
自定义一个日期选择器
  • 发表于 2年前
  • 阅读 6
  • 收藏 0
  • 点赞 2
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: 日期选择器

#import <UIKit/UIKit.h>

 

@interface DataPickerView : UIView

 

@property (nonatomic,copy) void(^callMeRemove)();

 

@end

 

 

 

#import "DataPickerView.h"

 

@interface DataPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

 

@property (nonatomic) UIPickerView   *pickView;

 

@property (nonatomic) UIView         *backdropView;

 

@property (nonatomic) NSMutableArray *pickerDataAray;

 

@property (nonatomic) UITableView    *tableView;

 

@property (nonatomic) NSMutableArray *years;

 

@property (nonatomic) NSMutableArray *mounths;

 

@property (nonatomic) NSMutableArray *dayes;

 

@property (nonatomic) NSInteger      whereYear;

 

@property (nonatomic) NSInteger      whereMouth;

 

@property (nonatomic) NSArray        *nibArray;

 

@property (nonatomic) NSInteger      page;

 

@property (nonatomic) NSInteger      numsOfPeople;

 

@property (nonatomic) NSString       *dicOfYear;

 

@property (nonatomic) NSString       *dicOfMouth;

 

@property (nonatomic) NSString       *dicOfDays;

 

@property (nonatomic) UIView         *bearView;

 

@property (nonatomic) NSInteger      tagOfTimeLabel;

 

@property (nonatomic) NSInteger      oldTimeBtnTag;

 

@end

 

 

@implementation DataPickerView

 

-(id)initWithFrame:(CGRect)frame

{

    if (self  = [super initWithFrame:frame]) {

        [self creatPickerView];

    }

    

    return self;

}

 

-(void)creatPickerView

{

 

    

    self.dicOfYear  = [Date_String  substringWithRange:NSMakeRange(0, 4)];

    self.dicOfMouth = [Date_String  substringWithRange:NSMakeRange(5, 2)];

    self.dicOfDays  = [Date_String  substringWithRange:NSMakeRange(8, 2)];

    self.whereYear  = [[Date_String substringWithRange:NSMakeRange(0, 4)] integerValue];

    self.years      = [NSMutableArray array];

    self.mounths    = [NSMutableArray array];

    self.dayes      = [NSMutableArray array];

    self.whereMouth = [[Date_String substringWithRange:NSMakeRange(5, 2)] integerValue];

    //日期选择器

    self.backdropView = [[UIView alloc] initWithFrame:self.bounds];

    self.backdropView.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.4];

    [self addSubview:self.backdropView];

    

    

    self.bearView = [[UIView alloc]initWithFrame:CGRectMake(30, 0, SCREEN_WIDTH-60, 320)];

    self.bearView.center = self.center;

    self.bearView.backgroundColor = WHITE_COLOR;

    [self.backdropView addSubview:self.bearView];

    

    [self getDatePickerData];

    

    NSArray *selectedNum = @[[Date_String substringWithRange:NSMakeRange(0 , 4)],[Date_String substringWithRange:NSMakeRange(5, 2)],[Date_String substringWithRange:NSMakeRange(8, 2)]];

    

    for (int index = 0; index<=2; index++) {

        self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-60)*index/3, 130, (SCREEN_WIDTH-60)/3, 120)];

        

        self.pickView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:0.3];

        self.pickView.delegate =self;

        self.pickView.dataSource = self;

        self.pickView.tag = 100+index;

        //_rowA = [selectedNum[index] integerValue]-1;

        if (index) {

            [self.pickView selectRow:[selectedNum[index] integerValue]-1 inComponent:0 animated:YES];

        }else{

            [self.pickView selectRow:30 inComponent:0 animated:YES];

        }

        [self.bearView addSubview:self.pickView];

        

    }

    

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20, 240+30, SCREEN_WIDTH-60-40, 30)];

    [button setTitleColor:WHITE_COLOR forState:UIControlStateNormal];

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

    [button addTarget:self action:@selector(makeSureHowTimes) forControlEvents:UIControlEventTouchUpInside];

    

    

    [button setBackgroundImage:[UIImage imageWithContentsOfFile:The_path_Resource(@"Button")] forState:UIControlStateNormal];

    button.layer.cornerRadius = 7.5;

    [self.bearView addSubview:button];

    

    UIImageView *diseIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-60, 110)];

    diseIV.image = [UIImage imageWithContentsOfFile:The_path_Resource(@"dise")];

    diseIV.userInteractionEnabled = YES;

    [self.bearView addSubview:diseIV];

    

    UIImageView *clockIV = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-60)-90*285/226, 20, 90*285/226, 90)];

    clockIV.image = [UIImage imageWithContentsOfFile:The_path_Resource(@"Clock")];

    clockIV.userInteractionEnabled = YES;

    [self.bearView addSubview:clockIV];

    

    

    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-60, 30)];

    lable.backgroundColor =[UIColor clearColor];

    lable.text = @"自选天数";

    lable.center = CGPointMake((SCREEN_WIDTH-60)/2, 25);

    lable.textColor = WHITE_COLOR;

    lable.textAlignment = NSTextAlignmentCenter;

    [self.bearView addSubview:lable];

    

    

    NSArray *placeHloders = @[@"点击选择开始日期",@"点击选择结束日期"];

    for (int index = 0; index <= 1; index++) {

        UIButton *timeLable = [[UIButton alloc]initWithFrame:CGRectMake(35, 50+30*index, SCREEN_WIDTH-130, 25)];

        timeLable.backgroundColor =[UIColor clearColor];

        [timeLable setTitle:placeHloders[index] forState:UIControlStateNormal];

        timeLable.titleLabel.tintColor = WHITE_COLOR;

        timeLable.layer.borderWidth = 1;

        timeLable.layer.borderColor = WHITE_COLOR.CGColor;

        timeLable.tag = 600+index;

        timeLable.titleLabel.textAlignment = NSTextAlignmentCenter;

        [timeLable addTarget:self action:@selector(makeSureWhichLabelTime:) forControlEvents:UIControlEventTouchUpInside];

        [self.bearView addSubview:timeLable];

    }

    

    

}

 

-(void)getDatePickerData

{

    NSString *stringDate = [NSString stringWithFormat:@"%@",[NSDate date]];

    int a = [[stringDate substringToIndex:4] intValue];

    

    for (int index = a-30; index<=a+30; index++) {

        [self.years addObject:[NSString stringWithFormat:@"%d",index]];

    }

    

    int index =1;

    while (index<=12) {

        [self.mounths addObject:[NSString stringWithFormat:@"%d",index]];

        index++;

    }

    

    for (int day = 1; day <= 31; day++) {

        [self.dayes addObject:[NSString stringWithFormat:@"%d",day]];

    }

    

    

}

 

#pragma mark - 日期选择器代理实现方法 -

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 1;

}

 

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

{

    

    switch (pickerView.tag) {

        case 100:

        {

            return self.years.count;

        }

            break;

        case 101:

        {

            return 12;

        }

            break;

            

        default:{

            

            NSInteger mounth = self.whereMouth;

            if (mounth==1||mounth==3||mounth==5||mounth==7||mounth==8||mounth==10||mounth==12) {

                

                return 31;

                

            }else if(mounth==2){

                NSInteger year =self.whereYear;

                if((year%4==0&&year%100!=0)||(year%400==0)) {

                    

                    return 29;

                    

                }else{

                    

                    return 28;

                    

                }

            }else{

                

                return 30;

                

            }

            

        }

            break;

    }

    

}

 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    switch (pickerView.tag) {

        case 100:

        {

            return [NSString stringWithFormat:@"%@年",self.years[row]];

        }

            break;

        case 101:

        {

            return [NSString stringWithFormat:@"%@月",self.mounths[row]];

        }

            break;

            

        default:{

            

            return [NSString stringWithFormat:@"%@日",self.dayes[row]];

            

        }

            break;

    }

}

 

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

{

    UILabel* pickerLabel = (UILabel*)view;

    if (!pickerLabel){

        pickerLabel = [[UILabel alloc] init];

        pickerLabel.adjustsFontSizeToFitWidth = YES;

        pickerLabel.textAlignment = NSTextAlignmentCenter;

        [pickerLabel setBackgroundColor:[UIColor clearColor]];

        pickerLabel.tag = 444;

        [pickerLabel setFont:[UIFont boldSystemFontOfSize:17]];

        

    }

    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];

    return pickerLabel;

}

 

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

{

    

    

    switch (pickerView.tag) {

        case 100:

        {

            self.whereYear =[self.years[row] integerValue];

            self.dicOfYear = self.years[row];

            

        }

            break;

        case 101:

        {

            self.whereMouth = row+1;

            self.dicOfMouth = [NSString stringWithFormat:@"%.2ld",row+1];

            

        }

            break;

            

        default:{

            NSString *str = [NSString stringWithFormat:@"%.2ld",row+1];

            self.dicOfDays = str;

            

        }

            

            

            break;

    }

    

    NSArray *textArr = @[self.dicOfYear,self.dicOfMouth,self.dicOfDays];

    

    if (self.tagOfTimeLabel==600) {

        UIButton *button = [self.bearView viewWithTag:600];

        [button setTitle:[textArr componentsJoinedByString:@"-"] forState:UIControlStateNormal];

        [OrderInstance sharedInstance].model.card_valid_date = [textArr componentsJoinedByString:@"-"];

        

    }else if(self.tagOfTimeLabel==601){

        UIButton *button = [self.bearView viewWithTag:601];

        [button setTitle:[textArr componentsJoinedByString:@"-"] forState:UIControlStateNormal];

        [OrderInstance sharedInstance].model.card_exp_date = [textArr componentsJoinedByString:@"-"];

        

       

    }

    

    

    [self.pickView reloadAllComponents];

    

}

 

-(void)makeSureWhichLabelTime:(UIButton *)button

{

    UIButton *btn = [self.bearView viewWithTag:self.oldTimeBtnTag];

    btn.layer.borderWidth = 1;

    button.layer.borderWidth = 3;

    self.tagOfTimeLabel = button.tag;

    self.oldTimeBtnTag = button.tag;

}

-(void)makeSureHowTimes

{

 

    

    if (_callMeRemove) {

        

        self.callMeRemove();

        

    }

    

}

 

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    if (_callMeRemove) {

        

        self.callMeRemove();

        

    }

}

 

共有 人打赏支持
粉丝 0
博文 13
码字总数 4174
×
默默沫沫shine
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: