自定義時(shí)間選擇器UIPickerView

使用方法:

@property (nonatomic, assign) QLChangeTimeView *chooseTimeView;

//時(shí)間選擇視圖

_chooseTimeView = [[NSBundle mainBundle]loadNibNamed:@"QLChangeTimeView" owner:self options:nil].lastObject;

_chooseTimeView.frame = CGRectMake( 時(shí)間選擇器的大小);

_chooseTimeView.titleArr = @[@"今天",@"明天"];

//當(dāng)前時(shí)間

NSDate *date = [NSDate date];

_chooseTimeView.nowDate = date;

_chooseTimeView.mintInterval = 10;

_chooseTimeView.leadTime = 20;

_chooseTimeView.block = ^(NSString *time) {

//time返回選擇的時(shí)間

};

//時(shí)間選擇視圖確認(rèn)按鈕

[_chooseTimeView.yesBtn addTarget:self action:@selector(yesBtnClick) forControlEvents:UIControlEventTouchUpInside];

//時(shí)間選擇視圖取消按鈕

[_chooseTimeView.noBtn addTarget:self action:@selector(noBtnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_chooseTimeView];


QLChangeTimeView內(nèi)容:

#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.每一列顯示什么內(nèi)容

- (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:@"%@點(diǎn)",_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;

}

//監(jiān)聽

- (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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容