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

.H 里面

typedef void (^Selectdate)(NSDate * selectdate);//時(shí)間

@interface DPFSelectDatePickerView : UIView

@property (nonatomic, strong) UIDatePicker *datePicker;//時(shí)間選擇器

@property (nonatomic, strong) NSDate *debtTimeDate;//時(shí)間選擇器

@property (nonatomic, strong) UIView *LineView;//線

@property (nonatomic, strong) UILabel *titleLabel;//標(biāo)題label

@property (nonatomic, strong) UIButton *closeButton;//關(guān)閉按鈕

@property (nonatomic, strong) UIButton *okButton;//確定按鈕

@property (nonatomic, copy) Selectdate selectdate;

+ (DPFSelectDatePickerView *)showWithTitle:(NSString *)title

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? selectdate:(Selectdate)selectdate;

@end


.M

@implementation DPFSelectDatePickerView

{

? ? float alertHeight;//彈框整體高度,默認(rèn)240

? ? UIView *alertView;//彈框視圖

}

+ (DPFSelectDatePickerView *)showWithTitle:(NSString *)title

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? selectdate:(Selectdate)selectdate{

? ? DPFSelectDatePickerView *alert = [[DPFSelectDatePickerView alloc] initWithTitle:title selectdate:selectdate];

? ? return alert;

}

- (instancetype)initWithTitle:(NSString *)title selectdate:(Selectdate)selectdate {

? ? if (self = [super init]) {

? ? ? ? self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];

? ? ? ? alertHeight = 240;



? ? ? ? _selectdate = [selectdate copy];


? ? ? ? alertView = [[UIView alloc] init];

? ? ? ? alertView.backgroundColor = [UIColor whiteColor];

? ? ? ? alertView.frame = CGRectMake(0, ([UIScreen mainScreen].bounds.size.height-alertHeight), [UIScreen mainScreen].bounds.size.width, alertHeight);

? ? ? ? [self addSubview:alertView];


? ? ? ? _LineView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1);

? ? ? ? _LineView = [[UILabel alloc] init];

? ? ? ? _LineView.backgroundColor = BDRColor_dddddd;

? ? ? ? [alertView addSubview:_LineView];


? ? ? ? _titleLabel = [[UILabel alloc] init];

? ? ? ? _titleLabel.frame = CGRectMake(0, 1, alertView.frame.size.width, 39);

? ? ? ? _titleLabel.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];

? ? ? ? _titleLabel.textColor = [UIColor blackColor];

? ? ? ? _titleLabel.font = [UIFont boldSystemFontOfSize:17];

? ? ? ? _titleLabel.textAlignment = NSTextAlignmentCenter;

? ? ? ? self.titleLabel.text = title;

? ? ? ? [alertView addSubview:_titleLabel];


? ? ? ? _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];

? ? ? ? _closeButton.frame = CGRectMake(0, 5, 48, 30);

? ? ? ? [_closeButton setTitle:@"取消" forState:UIControlStateNormal];

? ? ? ? [_closeButton setTitleColor:BDRColor_blue forState:UIControlStateNormal];

? ? ? ? _closeButton.titleLabel.font = [UIFont systemFontOfSize:15];

? ? ? ? [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];

? ? ? ? [alertView addSubview:_closeButton];


? ? ? ? _okButton = [UIButton buttonWithType:UIButtonTypeCustom];

? ? ? ? _okButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-48, 5,48, 30);

? ? ? ? [_okButton setTitle:@"確定" forState:UIControlStateNormal];

? ? ? ? [_okButton setTitleColor:BDRColor_blue forState:UIControlStateNormal];

? ? ? ? _okButton.titleLabel.font = [UIFont systemFontOfSize:15];

? ? ? ? [_okButton addTarget:self action:@selector(okAction) forControlEvents:UIControlEventTouchUpInside];

? ? ? ? [alertView addSubview:_okButton];


? ? ? ? _datePicker=[[UIDatePicker alloc]init];

? ? ? ? _datePicker.backgroundColor=[UIColor whiteColor];

? ? ? ? _datePicker.datePickerMode=UIDatePickerModeDate;

? ? ? ? _datePicker.maximumDate= [NSDate date];//今天

? ? ? ? NSString *minStr = @"1949-10-01";

? ? ? ? NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

? ? ? ? [dateFormatter setDateFormat:@"yyyy-MM-dd"];

? ? ? ? NSDate *minDate = [dateFormatter dateFromString:minStr];

? ? ? ? _datePicker.minimumDate=minDate;

? ? ? ? [_datePicker addTarget:self action:@selector(datePickerClick:) forControlEvents:UIControlEventValueChanged];

? ? ? ? _datePicker.frame=CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, alertHeight-40);

? ? ? ? [alertView addSubview:_datePicker];



? ? ? ? [self show];

? ? }

? ? return self;

}

- (void)show {

? ? self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

? ? [[UIApplication sharedApplication].keyWindow addSubview:self];


? ? alertView.alpha = 0.0;

? ? [UIView animateWithDuration:0.05 animations:^{

? ? ? ? alertView.alpha = 1;

? ? }];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

? ? UITouch *touch = [touches anyObject];

? ? CGPoint pt = [touch locationInView:self];

? ? if (!CGRectContainsPoint([alertView frame], pt)) {

? ? ? ? [self closeAction];

? ? }

}

-(void)datePickerClick:(id)sender

{

? ? UIDatePicker* control = (UIDatePicker*)sender;

? ? _debtTimeDate = control.date;



}

- (void)okAction{

? ? if (self.selectdate) {

? ? ? ? self.selectdate(_debtTimeDate);

? ? }

? ? [self closeAction];

}

- (void)closeAction {

? ? [UIView animateWithDuration:0.1 animations:^{

? ? ? ? self.alpha = 0;

? ? } completion:^(BOOL finished) {

? ? ? ? [self removeFromSuperview];

? ? }];

}

- (void)dealloc {

? ? //? ? NSLog(@"SelectAlert被銷毀了");

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市见坑,隨后出現(xiàn)的幾起案子嚷掠,更是在濱河造成了極大的恐慌,老刑警劉巖荞驴,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異贯城,居然都是意外死亡熊楼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進(jìn)店門能犯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鲫骗,“玉大人踩晶,你說我怎么就攤上這事执泰。” “怎么了渡蜻?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵术吝,是天一觀的道長。 經(jīng)常有香客問我茸苇,道長排苍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任学密,我火速辦了婚禮淘衙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘腻暮。我一直安慰自己彤守,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布哭靖。 她就那樣靜靜地躺著具垫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪款青。 梳的紋絲不亂的頭發(fā)上做修,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼饰及。 笑死蔗坯,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的燎含。 我是一名探鬼主播宾濒,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼屏箍!你這毒婦竟也來了绘梦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤赴魁,失蹤者是張志新(化名)和其女友劉穎卸奉,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體颖御,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡榄棵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了潘拱。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片疹鳄。...
    茶點(diǎn)故事閱讀 40,488評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖芦岂,靈堂內(nèi)的尸體忽然破棺而出瘪弓,到底是詐尸還是另有隱情,我是刑警寧澤禽最,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布腺怯,位于F島的核電站,受9級特大地震影響弛随,放射性物質(zhì)發(fā)生泄漏瓢喉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一舀透、第九天 我趴在偏房一處隱蔽的房頂上張望栓票。 院中可真熱鬧,春花似錦愕够、人聲如沸走贪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽坠狡。三九已至,卻和暖如春遂跟,著一層夾襖步出監(jiān)牢的瞬間逃沿,已是汗流浹背婴渡。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留凯亮,地道東北人边臼。 一個(gè)月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像假消,于是被迫代替她去往敵國和親柠并。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容