iOS一个弹出的时间选择器视图(UIDatePicker)
效果图:
文章图片
代码:
#define kScreen_Width [UIScreen mainScreen].bounds.size.width
#define kScreen_Height [UIScreen mainScreen].bounds.size.height#import @protocol SYDatePickerDelegate @optional
//当UIDatePicker值变化时所用到的代理
- (void)picker:(UIDatePicker *)picker ValueChanged:(NSDate *)date;
@end@interface SYDatePicker : UIView@property(weak, nonatomic) id delegate;
@property(strong, nonatomic) UIDatePicker *picker;
- (void)showInView:(UIView *)view withFrame:(CGRect)frame andDatePickerMode:(UIDatePickerMode)mode;
- (void)dismiss;
- (void)valueChanged:(UIDatePicker *)picker;
@end
#import "SYDatePicker.h"@implementation SYDatePicker- (void)showInView:(UIView *)view withFrame:(CGRect)frame andDatePickerMode:(UIDatePickerMode)mode{
self.frame = frame;
self.backgroundColor = [UIColor lightGrayColor];
if(!self.picker){
self.picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 46, frame.size.width, frame.size.height - 38)];
}self.picker.datePickerMode = mode;
[self.picker addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
[self addSubview:self.picker];
UIButton *btnDone = [UIButton buttonWithType:UIButtonTypeSystem];
btnDone.frame = CGRectMake(8, 8, 50, 30);
[btnDone setTitle:@"完成" forState:UIControlStateNormal];
[btnDone addTarget:self action:@selector(pickDone) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnDone];
[view addSubview:self];
self.frame = CGRectMake(0, kScreen_Height, kScreen_Width, 0);
[UIView animateWithDuration:0.3 animations:^{
self.frame = frame;
}];
}- (void)pickDone{
if (![self.picker respondsToSelector:@selector(valueChanged:)]) {
[self.delegate picker:self.picker ValueChanged:self.picker.date];
}
[self dismiss];
}- (void)dismiss{
[UIView animateWithDuration:0.3 animations:^{
self.frame = CGRectMake(0, kScreen_Height, kScreen_Width, 0);
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
源代码下载
【iOS一个弹出的时间选择器视图(UIDatePicker)】
推荐阅读
- 一个人的旅行,三亚
- 一个小故事,我的思考。
- 一个人的碎碎念
- 七年之痒之后
- 我从来不做坏事
- 异地恋中,逐渐适应一个人到底意味着什么()
- 迷失的世界(二十七)
- live|live to inspire 一个普通上班族的流水账0723
- 2020-04-07vue中Axios的封装和API接口的管理
- 遗憾是生活的常态,但孝顺这件事,我希望每一个人都不留遗憾