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

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

PyQt5 GUI 开发的基础知识

《PyQt5GUI开发的基础知识》Qt是一个跨平台的C++图形用户界面开发框架,支持GUI和非GUI程序开发,本文介绍了使用PyQt5进行界面开发的基础知识,包括创建简单窗口、常用控件、窗口属性设... 目录简介第一个PyQt程序最常用的三个功能模块控件QPushButton(按钮)控件QLable(纯文本

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部