iOS开发 选择日期的view 一(UIDatePicker的封装)

2024-03-05 04:18

本文主要是介绍iOS开发 选择日期的view 一(UIDatePicker的封装),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

之前写了个基于类簇的自定义选择日期的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)framedatePickerMode:(UIDatePickerMode)datePickerModelastDate:(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)framedatePickerMode:(UIDatePickerMode)datePickerModelastDate:(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:XGScreenBoundsdatePickerMode:UIDatePickerModeDatelastDate:self.choseDate];[choseView showView];__weak __typeof(self)weakSelf = self;[choseView confirmDate:^(NSDate *date) {weakSelf.choseDate = date;NSLog(@"当前选择的时间是==%@==",date);}];

其中的choseDate 也就是记录上次所选择的时间,就酱,还有很多待优化,以后有时间了再说...

效果:


这篇关于iOS开发 选择日期的view 一(UIDatePicker的封装)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/775314

相关文章

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

springboot日期格式化全局LocalDateTime详解

《springboot日期格式化全局LocalDateTime详解》文章主要分析了SpringBoot中ObjectMapper对象的序列化和反序列化过程,并具体探讨了日期格式化问题,通过分析Spri... 目录分析ObjectMapper与jsonSerializer结论自定义日期格式(全局)扩展利用配置

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

基于Python开发PPTX压缩工具

《基于Python开发PPTX压缩工具》在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,不便于传输和存储,所以本文将使用Python开发一个PPTX压缩工具,需要的可以了解下... 目录引言全部代码环境准备代码结构代码实现运行结果引言在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

Python 中 requests 与 aiohttp 在实际项目中的选择策略详解

《Python中requests与aiohttp在实际项目中的选择策略详解》本文主要介绍了Python爬虫开发中常用的两个库requests和aiohttp的使用方法及其区别,通过实际项目案... 目录一、requests 库二、aiohttp 库三、requests 和 aiohttp 的比较四、requ