使用方法:
@property (nonatomic, assign) QLChangeTimeView *chooseTimeView;
//时间选择视图
_chooseTimeView = [[NSBundle mainBundle]loadNibNamed:@"QLChangeTimeView" owner:self options:nil].lastObject;
_chooseTimeView.frame = CGRectMake( 时间选择器的大小);
_chooseTimeView.titleArr = @[@"今天",@"明天"];
//当前时间
NSDate *date = [NSDate date];
_chooseTimeView.nowDate = date;
_chooseTimeView.mintInterval = 10;
_chooseTimeView.leadTime = 20;
_chooseTimeView.block = ^(NSString *time) {
//time返回选择的时间
};
//时间选择视图确认按钮
[_chooseTimeView.yesBtn addTarget:self action:@selector(yesBtnClick) forControlEvents:UIControlEventTouchUpInside];
//时间选择视图取消按钮
[_chooseTimeView.noBtn addTarget:self action:@selector(noBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_chooseTimeView];
QLChangeTimeView内容:
#import "QLChangeTimeView.h"
@interface QLChangeTimeView()
@property (nonatomic, strong) NSMutableDictionary *dataDic;
@property (nonatomic, strong) NSDictionary *dic;
@property (nonatomic, strong) NSMutableDictionary *HHDic;
@property (nonatomic, strong) NSMutableDictionary *bHHDic;
@property (nonatomic, strong) NSMutableArray *MMArr;
@property (nonatomic, strong) NSMutableArray *mmArr;
@property (nonatomic, strong) NSString *aStr;
@property (nonatomic, strong) NSString *bStr;
@property (nonatomic, strong) NSString *cStr;
@end
@implementation QLChangeTimeView
- (NSDictionary*)dataDic {
if (!_dataDic) {
_dataDic = [NSMutableDictionary dictionary];
}
return _dataDic;
}
- (NSDictionary*)dic {
if (!_dic) {
_dic = [NSDictionary dictionary];
}
return _dic;}
-(NSMutableArray *)MMArr {
if (!_MMArr) {
_MMArr = [NSMutableArray array];
}
return _MMArr;}
-(NSMutableArray *)mmArr {
if (!_mmArr) {
_mmArr = [NSMutableArray array];
}
return _mmArr;
}
- (void)awakeFromNib {
[super awakeFromNib];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
[self addObserver:self forKeyPath:@"nowDate" options:NSKeyValueObservingOptionNew context:nil];
}
- (IBAction)noBtnClick:(id)sender {
}
- (IBAction)yesBtnClick:(id)sender {
_result = [NSString stringWithFormat:@"%@ %@:%@",_aStr,_bStr,_cStr]; MYLog(@"%@",_result);
_block(_result);
_aStr = nil;
_bStr = nil;
_cStr = nil;
}
#pragma mark -- UIPickerView
//1.有几列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
_dic = _dataDic[@"0"];
NSArray *arr = _dic.allKeys;
NSArray *result = [arr sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2]; //升序
}];
_MMArr = [result copy];
NSString *name = _MMArr[0];
_mmArr = _dic[name];
return 3;
}
//2.每一列有几行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
switch (component) {
case 0:
return _titleArr.count;
case 1:{
return _MMArr.count;
}
default:{
return _mmArr.count;
}
}
}
//3.每一列显示什么内容
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lblDate = [[UILabel alloc] init];
[lblDate setFont:[UIFont systemFontOfSize:16.0]];
[lblDate setTextColor:[UIColor blackColor]];
[lblDate setBackgroundColor:[UIColor clearColor]];
[lblDate setTextAlignment:NSTextAlignmentCenter];
if (component == 0) {
[lblDate setText:_titleArr[row]];
} else if (component == 1) {
[lblDate setText:[NSString stringWithFormat:@"%@点",_MMArr[row]]];
} else {
[lblDate setText:[NSString stringWithFormat:@"%@分",_mmArr[row]]];
}
return lblDate;
}
//选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
_dic = _dataDic[[NSString stringWithFormat:@"%ld",(long)row]];
NSArray *arr = _dic.allKeys;
NSArray *result = [arr sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2]; //升序
}];
_MMArr = [result copy];
NSString *name = _MMArr[row];
_mmArr = _dic[name];
[self.pickerView reloadComponent:1];
[self.pickerView reloadComponent:2];
[self.pickerView selectRow:0 inComponent:1 animated:YES];
[self.pickerView selectRow:0 inComponent:2 animated:YES];
_aStr = [NSString stringWithFormat:@"%@",_titleArr[row]];
_bStr = [NSString stringWithFormat:@"%02d",[_MMArr[0] intValue]];
_cStr = [NSString stringWithFormat:@"%02d",[_mmArr[0] intValue]];
} else if (component == 1) {
NSString *name = _MMArr[row];
_mmArr = _dic[name];
[self.pickerView reloadComponent:2];
_bStr = [NSString stringWithFormat:@"%02d",[_MMArr[row] intValue]];
_cStr = [NSString stringWithFormat:@"%02d",[_mmArr[0] intValue]];
[self.pickerView selectRow:0 inComponent:2 animated:YES];
} else {
_cStr = [NSString stringWithFormat:@"%02d",[_mmArr[row] intValue]];
}
}
//高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 40;
}
//监听
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context {
if ([keyPath isEqualToString:@"nowDate"]) {
NSDate *date = [change objectForKey:NSKeyValueChangeNewKey];
NSDate *testDate = [NSDate dateWithTimeInterval:60*_leadTime sinceDate:date];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.dateFormat = @"YYYY-MM-dd HH mm";
[df setLocale:[NSLocale currentLocale]];
NSString *dateStr = [df stringFromDate:testDate];
[self changeTimeWay:dateStr];
[self.pickerView reloadAllComponents];
[self.pickerView selectRow:0 inComponent:0 animated:YES];
[self.pickerView selectRow:0 inComponent:1 animated:YES];
[self.pickerView selectRow:0 inComponent:2 animated:YES];
}
}
- (void)changeTimeWay:(NSString *)dateStr {
NSMutableDictionary *bHHDic = [NSMutableDictionary dictionary];
NSMutableDictionary *HHDic = [NSMutableDictionary dictionary];
NSMutableArray *mmArr = [NSMutableArray array];
NSMutableArray *MMArr = [NSMutableArray array];
NSArray *dateArr = [dateStr componentsSeparatedByString:@" "];
_aStr = _aStr.length == 0?@"今天" : _aStr;
_bStr = _bStr.length == 0?[NSString stringWithFormat:@"%02d",[dateArr[1] intValue]] : _bStr;
_cStr = _cStr.length == 0?[NSString stringWithFormat:@"%02d",[dateArr[2] intValue]-[dateArr[2] intValue]%10] : _cStr;
for (int j = [dateArr[2] intValue]-[dateArr[2] intValue]%10; j < 60; j = j+_mintInterval) {
[MMArr addObject:[NSString stringWithFormat:@"%d",j]];
}
for (int j = 0; j < 60; j = j+_mintInterval) {
[mmArr addObject:[NSString stringWithFormat:@"%d",j]];
}
for (int i = [dateArr[1] intValue]; i < 24; i++) {
if (i == [dateArr[1] intValue]) {
[HHDic setObject:MMArr forKey:[NSString stringWithFormat:@"%02d",[dateArr[1] intValue]]];
} else {
[HHDic setObject:mmArr forKey:[NSString stringWithFormat:@"%02d",i]];
}
}
for (int i = 0; i < 24; i++) {
[bHHDic setValue:mmArr forKey:[NSString stringWithFormat:@"%02d",i]];
}
_dataDic = [NSMutableDictionary dictionaryWithCapacity:_titleArr.count];
for (int i = 0; i < _titleArr.count; i++) {
if (i == 0) {
[_dataDic setValue:HHDic forKey:[NSString stringWithFormat:@"%d",i]];
} else {
[_dataDic setValue:bHHDic forKey:[NSString stringWithFormat:@"%d",i]];
}
}
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"nowDate"];
}
@end
QLChangeTimeView下载地址:https://pan.baidu.com/s/1i4Aq7NR