iOS開發 選擇日期的view 一(UIDatePicker的封裝)
阿新 • • 發佈:2019-02-09
之前寫了個基於類簇的自定義選擇日期的view的封裝,現在看看,感覺程式碼挺操蛋的,還是抽出來,只顯示UIDatePicker的封裝吧
// // XGChoseDateView.h // XGDevelopDemo // // Created by 小廣 on 15/8/18. // Copyright © 2015年 小廣. All rights reserved. // 選擇日期的view #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, DateType) { DateTypeNormal, // 預設時間選擇(月日時分) DateTypeModeDate // 時間選擇(只有年月日) }; typedef void (^ConfirmDateBlock)(NSDate *date); @interface XGChoseDateView : UIView - (instancetype)initWithFrame:(CGRect)frame datePickerMode:(UIDatePickerMode)datePickerMode lastDate:(NSDate *)lastDate; /** * View顯示 */ - (void)showView; /** * 確認所選時間 * * @param block 傳出Date */ - (void)confirmDate:(ConfirmDateBlock)block; @end
.m裡面的 程式碼 有點多,也懶得優化,都是用的純程式碼
// // XGChoseDateView.m // XGDevelopDemo // // Created by 小廣 on 15/8/18. // Copyright © 2015年 小廣. All rights reserved. // 選擇日期的view #import "XGChoseDateView.h" #define XGButtonWidth 60.0 #define TopBarHeight 44.0 #define PickerHeight 216.0 #define XGScreenBounds [UIScreen mainScreen].bounds #define XGScreenWidth XGScreenBounds.size.width #define XGScreenHeight XGScreenBounds.size.height #define XGViewHeight CGRectGetHeight(XGScreenBounds) - 44 - 20 // 去掉導航條的高度 @interface XGChoseDateView () @property (nonatomic, strong) UIDatePicker *datePicker; @property (nonatomic, strong) UIView *bgView; // 按鈕背景View @property (nonatomic, strong) UIButton *cancelButton; // 取消按鈕 @property (nonatomic, strong) UIButton *confirmButton; // 確定按鈕 @property (nonatomic, strong) NSDate *currentDate; // 當前顯示時間 @property (nonatomic, strong) NSDate *lastDate; // 上次選擇時間 @property (nonatomic, assign) UIDatePickerMode datePickerMode; // datePicker顯示形式 @property (nonatomic, copy) ConfirmDateBlock block; @end @implementation XGChoseDateView #pragma mark - 對外方法 - (instancetype)initWithFrame:(CGRect)frame datePickerMode:(UIDatePickerMode)datePickerMode lastDate:(NSDate *)lastDate { self = [super initWithFrame:frame]; if (self) { _datePickerMode = datePickerMode; _lastDate = lastDate; [self initSubViews]; } return self; } // 顯示view(此方法是載入在window上 ,遮住導航條) - (void)showView { UIWindow * window = [UIApplication sharedApplication].windows[0]; [window addSubview:self]; } // 傳值到外面 - (void)confirmDate:(ConfirmDateBlock)block { self.block = block; } #pragma mark - 自定義方法 - (void)initSubViews { self.backgroundColor = [UIColor clearColor]; self.alpha = 0; [self addCoverView]; [self addBGView]; self.datePicker.datePickerMode = self.datePickerMode; self.datePicker.date = self.lastDate ? self.lastDate : [NSDate date]; self.currentDate = self.datePicker.date; } // 新增蒙板 - (void)addCoverView { UIView *coverView = [[UIView alloc] initWithFrame:self.frame]; coverView.alpha = 0.3; coverView.backgroundColor = [UIColor blackColor]; [self addSubview:coverView]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverViewDidTouch:)]; tapGesture.numberOfTapsRequired = 1; [coverView addGestureRecognizer:tapGesture]; } // 大view新增子view(為了做動畫) - (void)addBGView { [self addSubview:self.bgView]; [self addButtons]; [self.bgView addSubview:self.datePicker]; [self.datePicker addTarget:self action:@selector(datePickerValueChange:) forControlEvents:UIControlEventValueChanged]; [UIView animateWithDuration:0.25 animations:^{ self.alpha = 1.0; CGFloat posY = XGScreenHeight - (PickerHeight + TopBarHeight); self.bgView.frame = CGRectMake(0.0, posY, XGScreenWidth, (PickerHeight + TopBarHeight)); } completion:^(BOOL finished) { // }]; } // 新增按鈕 - (void)addButtons { CGFloat posY = 0.0; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, posY, XGScreenWidth, TopBarHeight)]; view.backgroundColor = UIColorFrom16RGB(0x334455); [self.bgView addSubview:view]; // 取消按鈕 self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.cancelButton.frame = CGRectMake(0.0, posY, XGButtonWidth, TopBarHeight); self.cancelButton.tag = 0; [self.cancelButton setBackgroundColor:[UIColor clearColor]]; [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal]; [self.cancelButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [self.bgView addSubview:self.cancelButton]; // 確定按鈕 CGFloat posX = XGScreenWidth - XGButtonWidth; self.confirmButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.confirmButton.frame = CGRectMake(posX, posY, XGButtonWidth, TopBarHeight); self.confirmButton.tag = 1; [self.confirmButton setBackgroundColor:[UIColor clearColor]]; [self.confirmButton setTitle:@"確定" forState:UIControlStateNormal]; [self.confirmButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [self.bgView addSubview:self.confirmButton]; } // 按鈕點選事件 - (void)buttonClick:(UIButton *)sender { if (sender.tag == 1) { if (!self.currentDate) { self.currentDate = [NSDate date]; } if (self.block) { self.block(self.currentDate); } } [self dismissContactView]; } // 蒙板手勢事件 - (void)coverViewDidTouch:(UITapGestureRecognizer *)sender { if (!self.currentDate) { self.currentDate = [NSDate date]; } if (self.block) { self.block(self.currentDate); } [self dismissContactView]; } // 移除view - (void)dismissContactView { kWeakSelf [UIView animateWithDuration:0.25 animations:^{ weakSelf.bgView.frame = CGRectMake(0.0, XGScreenHeight, XGScreenWidth, 0.0); weakSelf.alpha = 0; } completion:^(BOOL finished) { [weakSelf removeFromSuperview]; }]; } #pragma mark - 懶載入 // PickerView和按鈕的父檢視view(為了做動畫) - (UIView *)bgView { if (!_bgView) { _bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, XGScreenHeight, XGScreenWidth, (PickerHeight + TopBarHeight))]; _bgView.backgroundColor = [UIColor clearColor]; _bgView.clipsToBounds = YES; } return _bgView; } - (UIDatePicker *)datePicker { if (!_datePicker) { _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, TopBarHeight, XGScreenWidth, 216.0)]; _datePicker.backgroundColor = [UIColor whiteColor]; } return _datePicker; } #pragma mark - 代理方法 // UIDatePicker繫結方法 - (void)datePickerValueChange:(UIDatePicker *)sender { self.currentDate = sender.date; } @end
用法:
XGChoseDateView *choseView = [[XGChoseDateView alloc] initWithFrame:XGScreenBounds datePickerMode:UIDatePickerModeDate lastDate:self.choseDate]; [choseView showView]; __weak __typeof(self)weakSelf = self; [choseView confirmDate:^(NSDate *date) { weakSelf.choseDate = date; NSLog(@"當前選擇的時間是==%@==",date); }];
其中的choseDate 也就是記錄上次所選擇的時間,就醬,還有很多待優化,以後有時間了再說...
效果: