iOS时间选择器(轻模仿滴滴)

cocoaAhda.jpg

###项目中遇到一个时间选择器写了小半天记录一哈
还是先来效果图:


时间选择.png

没时间多说了 代码看下边
用法:

DCDataPickerView  dataPickerView = [[DCDataPickerView alloc] initWithFrame:CGRectMake(10,kScreenW - 330, kScreenH - 20, 240)];
dataPickerView.delegate = self;
 [self.view addSubview:self.dataPickerView];

源码:

DCDatePickerView.h

#import <UIKit/UIKit.h>
@class DCDataPickerView;
@protocol DCDatePickerViewDelegate <NSObject>
// 代理方法
-(void)pickerDate:(DCDataPickerView *)pickerArea dayStr:(NSString *)dayStr houStr:(NSString *)houStr minStr:(NSString *)minStr;
@end

@interface DCDataPickerView : UIView
@property(weak,nonatomic)id<DCDatePickerViewDelegate>delegate;

@end
DCDatePickerView.m

#import "DCDataPickerView.h"
#define ZY_THEMEColor  [UIColor colorWithRed:((0xfe8a4d & 0xFF0000) >> 16)/255.0 green:((0xfe8a4d & 0xFF00) >> 8)/255.0 blue:((0xfe8a4d & 0xFF)/255.0) alpha:1]

@interface DCDataPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

// 三个pickerView的数据源
@property(strong,nonatomic)NSMutableArray * firstArray;
@property(strong,nonatomic)NSMutableArray * secondArray;
@property(strong,nonatomic)NSMutableArray * thirdArray;
@property(strong,nonatomic)UIView  * pickerView;//用来放pickerView
@end

@implementation DCDataPickerView
{
    UIPickerView * dayPickerView;
    UIPickerView * houPickerView;
    UIPickerView * minPickerView;
    NSInteger      currentHour;   // (当前时间几点钟)
    NSInteger      currentMins;   // (当前点的分钟数)
    
    // 存选中后返回的的内容
    NSString     * dayStr;
    NSString     * houStr;
    NSString     * minStr;
}
// 初始化方法,外界就是用这个方法
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        dayStr = @"温馨提示,赋值错误";
        houStr = @"温馨提示,赋值错误";
        minStr = @"温馨提示,赋值错误";
        [self setOwn];                    // 设置self
        [self initDataSource];            // 初始化数据源
        [self initAroundTheView];         // UI
        [self initPickerView];            // pickerView
    }
    return self;
}

#pragma mark -- dataSource 
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == dayPickerView) {
        return self.firstArray.count;
    }
    if (pickerView == houPickerView) {
        return self.secondArray.count;
    }
    if (pickerView == minPickerView) {
        return self.thirdArray.count;
    }else return 1;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
#pragma make -- delegate
// 每行高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return (pickerView.frame.size.height-20) / 3;
}
// 选中某行的时候
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    // 滑动第一列清除第二三列数据,重新赋值
    if (pickerView == dayPickerView) {
        if (row == 0) {  // 当选中的row为第一行的时候也就是今天,需要加上现在
            if (![self.secondArray[0] isEqualToString:@"现在"]) { // 数据中没有“现在”
                [self.secondArray removeAllObjects];  

                [self.secondArray insertObject:@"现在" atIndex:0];
                for (int i = 0; i < 24 ; i++) {  // 24小时,根据当前时间赋值
                    if (i > currentHour) {
                        if (i < 10) {
                            [self.secondArray addObject:[NSString stringWithFormat:@"0%ld点",(long)i]];
                        }else{
                            [self.secondArray addObject:[NSString stringWithFormat:@"%ld点",(long)i]];
                        }
                    }
                }

            }
            if (![self.thirdArray[0] isEqualToString:@"现在"]) {
                [self.thirdArray removeAllObjects];
                [self.thirdArray insertObject:@"现在" atIndex:0];
                for (int i = 0; i < 60; i+=10) {
                    if (i > currentMins) {
                        if (i / 10 == 0) {
                            [self.thirdArray addObject:[NSString stringWithFormat:@"0%d分",i]];
                        }else{
                            [self.thirdArray addObject:[NSString stringWithFormat:@"%d分",i / 10 * 10]];
                        }
                    }
                }
            }
        }else{
            // 选中的不是第一行的时候也就是明天后天就需要删除现在,并且改成24小时
            [self.secondArray removeAllObjects];
            [self.thirdArray removeAllObjects];
            for (int i = 0; i < 6; i++) {
                if (i == 0) {
                    [self.thirdArray addObject:[NSString stringWithFormat:@"0%d分",i]];
                }else{
                    [self.thirdArray addObject:[NSString stringWithFormat:@"%d分",i * 10]];
                }
            }
            for (int i = 0; i < 24 ; i++) {
                    if (i < 10) {
                        [self.secondArray addObject:[NSString stringWithFormat:@"0%ld点",(long)i]];
                    }else{
                        [self.secondArray addObject:[NSString stringWithFormat:@"%ld点",(long)i]];
                    }
            }
            
        }
        [houPickerView reloadAllComponents];
        [minPickerView reloadAllComponents];
    }
    if (pickerView == houPickerView) {
        if (row == 0) {  // 当选中的row为第一行的时候也就是今天,需要加上现在
            if (![self.thirdArray[0] isEqualToString:@"现在"]) {
                [self.thirdArray removeAllObjects];
                [self.thirdArray insertObject:@"现在" atIndex:0];
                for (int i = 0; i < 60; i+=10) {
                    if (i > currentMins) {
                        if (i / 10 == 0) {
                            [self.thirdArray addObject:[NSString stringWithFormat:@"0%d分",i]];
                        }else{
                            [self.thirdArray addObject:[NSString stringWithFormat:@"%d分",i / 10 * 10]];
                        }
                    }
                }
            }
        }else{
            [self.thirdArray removeAllObjects];
            for (int i = 0; i < 6; i++) {
                if (i == 0) {
                    [self.thirdArray addObject:[NSString stringWithFormat:@"0%d分",i]];
                }else{
                    [self.thirdArray addObject:[NSString stringWithFormat:@"%d分",i * 10]];
                }
            }
        }
        [minPickerView reloadAllComponents];
    }
    [self reloaData];  // 获取选中的时间
}
// 选中row样式
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    // 选中的一行
    UILabel *selectedLabel = (UILabel *)[pickerView viewForRow:row forComponent:component];
    selectedLabel.textColor = ZY_THEMEColor;
    
    // 分割线设置
    for(UIView *singleLine in pickerView.subviews){
        if (singleLine.frame.size.height < 1){
            singleLine.backgroundColor = ZY_THEMEColor;
        }
    }
    
    UILabel * rowLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 1, (self.frame.size.width - 200) / 3, self.frame.size.height / 3 - 2)];
    rowLabel.textAlignment = NSTextAlignmentCenter;
    rowLabel.backgroundColor = [UIColor whiteColor];
    rowLabel.adjustsFontSizeToFitWidth = YES;
    rowLabel.font = [UIFont systemFontOfSize:15];
    rowLabel.textColor = [UIColor blackColor];
    rowLabel.backgroundColor = [UIColor clearColor];
    [rowLabel sizeToFit];

    if (pickerView == dayPickerView) {
        rowLabel.text = self.firstArray[row];
        [self reloaData];
        return rowLabel;
    }
    if (pickerView == houPickerView) {
        rowLabel.text = self.secondArray[row];
        [self reloaData];
        return rowLabel;
    }
    if (pickerView == minPickerView) {
        rowLabel.text = self.thirdArray[row];
        [self reloaData];
        return rowLabel;
    }else return nil;
}
#pragma mark -- 数据
-(void)initDataSource{
    // 第一列的数据源
    self.firstArray = [NSMutableArray arrayWithObjects:@"今天",@"明天",@"后天", nil];
    self.secondArray = [NSMutableArray array];
    self.thirdArray  = [NSMutableArray array];
    
    // 第二第三列的
    NSDate * date = [NSDate date];  // 当前时间
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"HH"]; // 当前几点
    NSInteger currentHh  = [dateFormatter stringFromDate:date].integerValue;
    [dateFormatter setDateFormat:@"mm"];// 当前点几分钟
    NSInteger currentMn  = [dateFormatter stringFromDate:date].integerValue;
    
    currentHour = currentHh;
    currentMins = currentMn;

    [self.secondArray addObject:@"现在"]; 
    for (int i = 0; i < 24 ; i++) {
        if (i > currentHh) {
            if (i < 10) {
                [self.secondArray addObject:[NSString stringWithFormat:@"0%ld点",(long)i]];
            }else{
                [self.secondArray addObject:[NSString stringWithFormat:@"%ld点",(long)i]];
            }
        }
    }
    
    [self.thirdArray addObject:@"现在"];
    for (int i = 0; i < 60; i+=10) {
        if (i > currentMn) {
            if (i / 10 == 0) {
                [self.thirdArray addObject:[NSString stringWithFormat:@"0%d分",i]];
            }else{
                [self.thirdArray addObject:[NSString stringWithFormat:@"%d分",i / 10 * 10]];
            }
        }
    }
}

#pragma mark -- UI绘制
-(void)initPickerView{
    
    CGFloat intervalSize = 50;
    CGFloat pickerWidth = (self.pickerView.frame.size.width - 200) /3;
    CGFloat pickerHeigh = self.pickerView.frame.size.height - 20;
    
    dayPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(50, 10, pickerWidth,pickerHeigh)];
    dayPickerView.delegate = self;
    dayPickerView.dataSource = self;
    [self.pickerView addSubview:dayPickerView];
    
    houPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(dayPickerView.frame)+intervalSize, 10, pickerWidth, pickerHeigh)];
    houPickerView.delegate = self;
    houPickerView.dataSource = self;
    [self.pickerView addSubview:houPickerView];
    
    minPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(houPickerView.frame)+intervalSize, 10, pickerWidth, pickerHeigh)];
    minPickerView.delegate = self;
    minPickerView.dataSource = self;
    [self.pickerView addSubview:minPickerView];
}


-(void)initAroundTheView{
    
    CGFloat  titW    = self.frame.size.width;
    CGFloat  titH    = self.frame.size.height * 0.15;
    CGFloat  pickerH = self.frame.size.height *0.65;
    CGFloat  lineH   = 1;
    CGFloat  bottomH = self.frame.size.height - titH - pickerH - lineH * 2;
    
    
    // 标题View
    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,titW, titH)];
    lab.text = @"选择预约时间";
    lab.textColor = [UIColor darkGrayColor];
    lab.font = [UIFont systemFontOfSize:13];
    lab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:lab];

    
    // 上边的分割线
    UIView * topLine = [[UIView alloc]initWithFrame:CGRectMake(0,titH, titW, 1)];
    topLine.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.7];
    [self addSubview:topLine];
    
    
    // pickerView
    self.pickerView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topLine.frame), titW, pickerH)];
    [self addSubview:self.pickerView];

    
    // 下边的分割线
    UIView * bottomLine = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.pickerView.frame), titW, 1)];
    bottomLine.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.7];
    [self addSubview:bottomLine];
    
    
    // 最下边装btn的View
    CGFloat bottomViewHeight = self.frame.size.height * 0.21;
    UIView * bottomView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(bottomLine.frame), titW, bottomH)];
    [self addSubview:bottomView];
    
    
    // 两个btn之间的分割线
    UIView * verticalLine = [[UIView alloc]initWithFrame:CGRectMake(titW / 2 -0.5,0, 1,bottomViewHeight)];
    verticalLine.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.7];
    [bottomView addSubview:verticalLine];

    // 左边的btn
    CGFloat btnW = (self.frame.size.width -1)/2;
    UIButton * leftBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftBtn setTitleColor:[UIColor darkGrayColor] forState:(UIControlStateNormal)];
    [leftBtn setTitle:@"取消" forState:(UIControlStateNormal)];
    leftBtn.tag = 100000;
    leftBtn.frame = CGRectMake(0,0, btnW, bottomViewHeight);
    [leftBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [bottomView addSubview:leftBtn];
    
    // 右边的btn
    UIButton * rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightBtn setTitleColor:ZY_THEMEColor forState:(UIControlStateNormal)];
    [rightBtn setTitle:@"确定" forState:(UIControlStateNormal)];
    rightBtn.tag = 100001;
    rightBtn.frame = CGRectMake(btnW + 1,0, btnW, bottomViewHeight);
    [rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [bottomView addSubview:rightBtn];
}

// btn事件
-(void)btnAction:(UIButton *)btn{
    
    if (btn.tag == 100000) {
        [self removeFromSuperview];
    }else{

        if (_delegate) {
            [_delegate pickerDate:self dayStr:dayStr houStr:houStr minStr:minStr];
        }
        [self removeFromSuperview];
    }
}

#pragma mark -- 自己的设置
-(void)setOwn{
    self.layer.cornerRadius = 8;
    self.layer.masksToBounds = YES;
}

#pragma mark -- 获取数据
-(void)reloaData{
    
    NSInteger dayIndex = [dayPickerView selectedRowInComponent:0];
    NSInteger houIndex = [houPickerView selectedRowInComponent:0];
    NSInteger minIndex = [minPickerView selectedRowInComponent:0];
    
    dayStr   = self.firstArray[dayIndex];
    houStr   = self.secondArray[houIndex];
    minStr = self.thirdArray[minIndex];
    
}
@end

最近状态比较差,生活和身心
Last:Now that I can't grasp the fate,It would be,Life and death have life rich in days!

cocoaAhda.jpg

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,355评论 0 23
  • 这个篇文章只有短短两万多字,内容却意外丰富。里面许多看法和我平时所想相同,虽然有些地方太过于绝对,我并不认同,但不...
    琉璃色的猫阅读 385评论 0 0
  • 其实每个人都做着类似的事情,得到不同的结果,问个为什么,依然会得到不同的结果。 在未来的路上,会经常出现岔路口吧,...
    聂一一阅读 142评论 0 0
  • 苏莫自那日起,做很多事都漫不经心,白蓁和莫逸辰的那一幕幕,都深深刺痛了他的双眼,他的心无比挣扎,却被理智拉回现实。...
    未慈阅读 538评论 0 2