当前位置: 首页 > 编程日记 > 正文

OC封装时间选择器

#import <UIKit/UIKit.h>


@protocol TimeDatePickerViewDelegate <NSObject>


//必须实现的两个协议

@required

- (void)changeTime : (NSDate *)date;//当时改变时出发

- (void)daterMine : (NSDate *)date;//更确定时间

@end


@interface TimeDatePickerView :UIView


//快速创建

+ (instancetype)datePickerWithType:(UIDatePickerMode) type ;

//初始化方法

- (instancetype)initWithFrame:(CGRect)frame type:(UIDatePickerMode)type;

//和代理营运而生的block

@property (nonatomic,copy)void(^changeTimeBlock) (NSDate *date);

@property (nonatomic,copy)void(^determineBlock) (NSDate *date);

//显示

- (void)show;

//设置初始时间

- (void)setNowTime:(NSString *)dateStr;

//可选的最大和最小时间

@property (nonatomic,strong)NSDate *optionalMaxDate;

@property (nonatomic,strong)NSDate *optionalMinDate;

//设置自定义标题

@property (nonatomic,copy)NSString *title;

// NSDate --> NSString

- (NSString*)stringFromDate:(NSDate*)date;

//NSDate <-- NSString

- (NSDate*)dateFromString:(NSString*)dateString;

@property(assign ,nonatomic)id<TimeDatePickerViewDelegate>delegate;



@end







#import "TimeDatePickerView.h"


#define kZero 0

#define kFullWidth [UIScreen mainScreen].bounds.size.width

#define kFullHeight [UIScreen mainScreen].bounds.size.height


#define kDatePicY kFullHeight/3*2

#define kDatePicHeight kFullHeight/3


#define kDateTopBtnY kDatePicY - 30

#define kDateTopBtnHeight 30


#define kDateTopRightBtnWidth kDateTopLeftBtnWidth

#define kDateTopRightBtnX kFullWidth - 0 - kDateTopRightBtnWidth


#define kDateTopLeftbtnX  0

#define kDateTopLeftBtnWidth kFullWidth/6


@interface TimeDatePickerView()


@property (nonatomic,strong)UIDatePicker *dateP;

@property (nonatomic,strong)UIView *groundV;

@property (nonatomic,strong)UIButton *leftBtn;

@property (nonatomic,strong)UIButton *rightBtn;

@property (nonatomic,strong)UIView *topView;

@property (nonatomic,assign)UIDatePickerMode type;

@property (nonatomic,strong)UILabel *titleLabel;


@end



@implementation TimeDatePickerView


+ (instancetype)datePickerWithType:(UIDatePickerMode)type {

    TimeDatePickerView *datePicker = [[TimeDatePickerViewalloc] initWithFrame:[UIScreenmainScreen].boundstype:type];

    

    return datePicker;

}


- (instancetype) initWithFrame:(CGRect)frame type:(UIDatePickerMode)type {

    self = [superinitWithFrame:frame];

    if (self) {

        self.type = type;

        [selfaddSubview:self.groundV];

        [selfaddSubview:self.dateP];

        [selfaddSubview:self.topView];

        [selfaddSubview:self.leftBtn];

        [selfaddSubview:self.rightBtn];

    }

    returnself;

}


- (UIDatePicker *)dateP {

    if (!_dateP) {

        self.dateP = [[UIDatePickeralloc] initWithFrame:CGRectMake(kZero,kDatePicY, kFullWidth,kDatePicHeight)];

        self.dateP.backgroundColor = [UIColorwhiteColor];

        self.dateP.datePickerMode =self.type;

        self.dateP.locale = [[NSLocalealloc] initWithLocaleIdentifier:@"zh_CHS_CN"];

         [self.datePaddTarget:selfaction:@selector(handleDateP:)forControlEvents:UIControlEventValueChanged];

    }

    return_dateP;

}


- (UIView *)groundV {

    if (!_groundV) {

        self.groundV = [[UIViewalloc]initWithFrame:self.bounds];

        self.groundV.backgroundColor = [UIColorclearColor];

        self.groundV.alpha =0.7;

    }

    return_groundV;

}

//取消按钮

- (UIButton *)leftBtn{

    if (!_leftBtn) {

        self.leftBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        self.leftBtn.frame = CGRectMake(kDateTopLeftbtnX,kDateTopBtnY, kDateTopLeftBtnWidth,kDateTopBtnHeight);

        [self.leftBtnsetTitle:@"取消"forState:UIControlStateNormal];

        [self.leftBtnsetTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

        //        self.leftBtn.backgroundColor=[UIColor cyanColor];

        

        [self.leftBtnaddTarget:selfaction:@selector(handleDateTopViewLeft)forControlEvents:UIControlEventTouchUpInside];

    }

    return_leftBtn;

}

//确定按钮

- (UIButton *)rightBtn {

    if (!_rightBtn) {

        self.rightBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        self.rightBtn.frame = CGRectMake(kDateTopRightBtnX,kDateTopBtnY, kDateTopRightBtnWidth,kDateTopBtnHeight);

        [self.rightBtnsetTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

        //        self.rightBtn.backgroundColor=[UIColor cyanColor];

        [self.rightBtnsetTitle:@"确定"forState:UIControlStateNormal];

        [self.rightBtnaddTarget:selfaction:@selector(handleDateTopViewRight)forControlEvents:UIControlEventTouchUpInside];

    }

    return_rightBtn;

}


- (UIView *)topView {

    if (!_topView) {

        self.topView = [[UIViewalloc]initWithFrame:CGRectMake(kZero,kDateTopBtnY, kFullWidth,kDateTopBtnHeight)];

        self.topView.backgroundColor = [UIColorblackColor];

        

        _titleLabel =[[UILabelalloc]initWithFrame:CGRectMake(0,0, kFullWidth-2*(kDateTopLeftbtnX+kDateTopLeftBtnWidth) , kDateTopBtnHeight)];

        _titleLabel.text =@"选择时间";

        _titleLabel.textAlignment =NSTextAlignmentCenter ;

        _titleLabel.textColor =[UIColorwhiteColor];

        _titleLabel.font = [UIFontsystemFontOfSize:15.0f];

        _titleLabel.center =CGPointMake(_topView.frame.size.width/2,kDateTopBtnHeight/2);

        

        [self.topViewaddSubview:_titleLabel];

    }

    return_topView;

}


- (void)setOptionalMaxDate:(NSDate *)optionalMaxDate{

    _optionalMaxDate = optionalMaxDate;

    self.dateP.maximumDate = optionalMaxDate;

}


- (void)setOptionalMinDate:(NSDate *)optionalMinDate{

    _optionalMinDate = optionalMinDate;

    self.dateP.minimumDate = optionalMinDate;

}


- (void)setTitle:(NSString *)title{

    _title = title;

    _titleLabel.text = title;

}


- (void)setNowTime:(NSString *)dateStr{

    

    [self.datePsetDate:[selfdateFromString:dateStr] animated:YES];

}


- (void)show{

    [[UIApplicationsharedApplication].keyWindowaddSubview:self];

}


- (void)end{

    [selfremoveFromSuperview];

}


- (void)handleDateP :(NSDate *)date {

    

    if (self.changeTimeBlock) {

        self.changeTimeBlock(self.dateP.date);

    }

    

    if ([self.delegaterespondsToSelector:@selector(changeTime:)]) {

        [self.delegatechangeTime:self.dateP.date];

    }

    

}


- (void)handleDateTopViewLeft {

    [selfend];

}


- (void)handleDateTopViewRight {

    

    if (self.determineBlock) {

        self.determineBlock(self.dateP.date);

    }

    

    if ([self.delegaterespondsToSelector:@selector(determine:)]) {

        [self.delegatedaterMine:self.dateP.date];

    }

    [selfend];

}


// NSDate --> NSString

- (NSString*)stringFromDate:(NSDate*)date{

    

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

    switch (self.type) {

        caseUIDatePickerModeTime:

            [dateFormatter setDateFormat:@"HH:mm"];

            break;

        caseUIDatePickerModeDate:

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

            break;

        caseUIDatePickerModeDateAndTime:

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

            break;

        caseUIDatePickerModeCountDownTimer:

            [dateFormatter setDateFormat:@"HH:mm"];

            break;

        default:

            break;

    }

    NSString *destDateString = [dateFormatterstringFromDate:date];

    

    return destDateString;

    

}


//NSDate <-- NSString

- (NSDate*)dateFromString:(NSString*)dateString{

    

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

    switch (self.type) {

        caseUIDatePickerModeTime:

            [dateFormatter setDateFormat:@"HH:mm"];

            break;

        caseUIDatePickerModeDate:

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

            break;

        caseUIDatePickerModeDateAndTime:

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

            break;

        caseUIDatePickerModeCountDownTimer:

            [dateFormatter setDateFormat:@"HH:mm"];

            break;

        default:

            break;

    }

    NSDate *destDate= [dateFormatterdateFromString:dateString];

    

    return destDate;

}





@end






相关文章:

银行卡大小的充电宝,买就送耳机!

每个人的朋友圈和微博上似乎都有那么几个活得让人羡慕的朋友他们的生活看起来不仅精致&#xff0c;还很丰富多彩从早上第一刻就开始了↓出门旅游新一天的穿搭逆天朋友团咖啡馆到书店逼格十足的日料夜景太迷人忍不住发个小视频最后一定不要错过傍晚的夕阳&#xff0c;真的好上镜…

C++中插件使用举例

插件并不是在构建时链接的&#xff0c;而是在运行时发现并加载的。因此&#xff0c;用户可以利用你定义好的插件API来编写自己的插件。这样他们就能以指定方式扩展API的功能。插件库是一个动态库&#xff0c;它可以独立于核心API编译&#xff0c;在运行时根据需要显示加载。不过…

C和C++安全编码笔记:指针诡计

指针诡计(pointer subterfuge)是通过修改指针值来利用程序漏洞的方法的统称。 可以通过覆盖函数指针将程序的控制权转移到攻击者提供的外壳代码(shellcode)。当程序通过函数指针执行一个函数调用时&#xff0c;攻击者提供的代码将会取代原本希望执行的代码而得到执行。 对象指…

runLoop和runtime的分析

一.RunLoop: Runloop是事件接收和分发机制的一个实现。 Runloop提供了一种异步执行代码的机制&#xff0c;不能并行执行任务。 在主队列中&#xff0c;Main RunLoop直接配合任务的执行&#xff0c;负责处理UI事件、定时器以及其他内核相关事件。 (1).RunLoop的主要目的&#…

脑出血遇到深度学习,是否可以无所遁形?

近期大家对身体健康这个话题格外关注&#xff0c;而我们今天公开课的主题也恰巧与此不谋而合。我国脑卒的发病率已经超过心血管疾病&#xff0c;成为致死、致残率最高的疾病&#xff0c;并且发病率呈逐年上升的趋势&#xff0c;此外脑血管病和颅内肿瘤等脑部疾病也危害人们的健…

Cloudera Manager 5.3 和 CDH5.3.0 本地(离线)

为什么80%的码农都做不了架构师&#xff1f;>>> 声明一下&#xff1a;http://my.oschina.net/dataRunner/blog/369129 是本人所写&#xff0c;并非抄袭。 有部分内容来自 http://www.wangyongkui.com/hadoop-cdh5/ 这个文件是根据官网操作&#xff0c;翻译的不…

万字长文详解如何用Python玩转OpenGL | CSDN 博文精选

作者 | 天元浪子来源 | CSDN博文精选【编者按】OpenGL&#xff08;开放式图形库&#xff09;&#xff0c;用于渲染 2D、3D 矢量图形的跨语言、跨平台的应用程序编程接口&#xff0c;C、C、Python、Java等语言都能支持 OpenGL。本文作者以 Python 语法为例&#xff0c;用两万字详…

模仿视频抓帧实现

路口或某些场所可能并不会把从摄像头获取到的视频全部存储下来或对所有的视频帧进行处理&#xff0c;即摄像设备是一直处于打开状态&#xff0c;可能会根据需要间隔性的抓取其中一帧&#xff0c;或当某事件触发时才会抓取当前的一帧数据进行处理。这里使用两个线程来模仿此场景…

iOS--MD5加密封装

#import <Foundation/Foundation.h> interface MD5 : NSObject /** * md5加密 * * param inPutText 需要加密的字符串 * * return 加密好的字符串 */ (NSString *)md5:(NSString *)inPutText; end #import "MD5.h" #import "CommonCrypto/CommonDiges…

Akka路由_RoundRobinRoutingLogic

2019独角兽企业重金招聘Python工程师标准>>> Akka路由_RoundRobinRoutingLogic 使用Round Robin算法的Router&#xff0c;代码中有注释&#xff0c;基本和上篇文章中的代码一样 http://my.oschina.net/xinxingegeya/blog/369721&#xff0c; 具体如下&#xff0c;关…

iOS ---网络请求封装(自动缓存与手动缓存)

#import <Foundation/Foundation.h> interface WNetworkCache : NSObject /** * 手动写入/更新缓存 * * param jsonResponse 要写入的数据 * param URL 请求URL * * return 是否写入成功 */ (BOOL)saveJsonResponseToCacheFile:(id)jsonResponse andURL:(NSStrin…

Windows下获取视频设备的一种改进实现

之前在https://blog.csdn.net/fengbingchun/article/details/102806822中介绍过在Windows下获取视频设备列表的方法。其实那种实现方法是有缺陷的&#xff0c;当PC机上连接多个视频设备&#xff0c;并且其中有设备处于启动运行状态时&#xff0c;再调用相关接口获取视频设备可能…

最新单步目标检测框架,引入双向网络,精度和速度均达到不错效果

作者 | Tiancai Wang等译者 | 路一直都在出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;one-stage的目标检测方法因其具有实时性强、检测精度高等特点&#xff0c;近年来受到广泛关注。目标检测包括分类和定位两个子任务&#xff0c;通常来说&#xff0c;one-stage目…

基于Sentinel的Redis3.2高可用方案

默认情况下&#xff0c;Redis node和sentinel的protected-mode都是yes&#xff0c;在搭建集群时&#xff0c;若想从远程连接redis集群&#xff0c;需要将redis.conf和sentinel.conf的protected-mode修改为no&#xff0c;若只修改redis node&#xff0c;从远程连接sentinel后&am…

从YARN迁移到k8s,滴滴机器学习平台二次开发是这样做的

整理 | 夕颜出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;【导读】人工智能时代&#xff0c;机器学习已经渗透进每个领域&#xff0c;改变了这些领域的业务模式、技术架构以及方法论。随着深度学习技术近年来快速发展&#xff0c;高效、易用的机器学习平台对于互联…

最新 macOS Sierra 10.12.3 安装CocoaPods及使用详解

cocoapods官网&#xff1a;https://guides.cocoapods.org 一、什么是CocoaPods 每种语言发展到一个阶段&#xff0c;就会出现相应的依赖管理工具&#xff0c;例如 Java 语言的 Maven&#xff0c;nodejs 的 npm。随着 iOS 开发者的增多&#xff0c;业界也出现了为 iOS 程序提供…

libusb中的热插拔使用举例

以下为判断usb设备是插入还是拔出状态(热插拔)的测试代码&#xff1a; 在Windows下是不支持的&#xff0c;在Linux是支持的&#xff0c;下一个版本可能会支持Windows下的热插拔&#xff1a; #include <chrono> #include <thread> #include <iostream> #incl…

C++复制控制:拷贝构造函数

一、拷贝构造函数是一种特殊构造函数&#xff0c;具有单个形参&#xff0c;该形参&#xff08;常用const修饰&#xff09;是对该类类型的引用。与默认构造函数一样 &#xff0c;拷贝构造函数可由编译器隐式调用。拷贝构造函数应用的场合为&#xff1a; &#xff08;1&#xff0…

关于IOS获取本地通讯录信息(包含iOS9.0前后)

在ios开发当中&#xff0c;获取用户本地的通讯录功能愈加频繁的出现&#xff0c;七两自己也在自己公司的项目当中遇到的获取本地的通讯录信息的功能&#xff08;俗称“种子用户功能”&#xff0c;太可怕了&#xff09;。对此七两总结了自己使用本地通讯录时的注意点&#xff0c…

C和C++安全编码笔记:动态内存管理

4.1 C内存管理&#xff1a; C标准内存管理函数&#xff1a; (1).malloc(size_t size)&#xff1a;分配size个字节&#xff0c;并返回一个指向分配的内存的指针。分配的内存未被初始化为一个已知值。 (2).aligned_alloc(size_t alignment, size_t size)&#xff1a;为一个对象…

作为一名程序员,数学到底对你有多重要?

最近在知乎上看到一个贴子&#xff0c;看完后我沉默了.....沉思后想想&#xff0c;其实每个行业都会分等级&#xff0c;程序员也不例外&#xff01;说好听一点的叫工程师&#xff0c;普通一点的叫程序员&#xff0c;差一点的叫码农&#xff0c;更差的还会叫码畜&#xff0c;码奴…

经典SQL(sqlServer)

一、基础 1、说明&#xff1a;创建数据库CREATE DATABASE database-name 2、说明&#xff1a;删除数据库drop database dbname3、说明&#xff1a;备份sql server--- 创建 备份数据的 deviceUSE masterEXEC sp_addumpdevice disk, testBack, c:\mssql7backup\MyNwind_1.dat--- …

iOS UITextField输入框随键盘弹出界面上移

//点击输入框界面跟随键盘上移 - (void)textFieldDidBeginEditing:(UITextField *)textField { CGRect frame textField.frame; int offSet frame.origin.y 70 - (self.view.frame.size.height - 216.0); //iphone键盘高度为216.iped键盘高度为352 [UIView beginAnimations:…

IEEE分享 | 机器学习在领英的规模化应用

人工智能和机器学习仍然是全球持续增长的领域之一&#xff0c;近年来涌现出越来越多本科生或者非人工智能专业出身的工程师&#xff0c;他们努力学习和使用技术来改进产品&#xff0c;几乎每天都有新的机器学习技术和框架发布。这篇文章将讨论领英如何规模化利用技术&#xff0…

GitHub/GitLab/Gitee中项目互拷贝后仍保留历史提交记录的方法

GitHub、GitLab、Gitee等在同一个网站中执行复制或拷贝一个已有项目到一个新项目比较简单&#xff0c;因为它们在每一个项目上都有一个Fork按钮&#xff0c;直接点击此Fork按钮即可&#xff0c;Fork后的新项目会保留原有项目的历史提交记录。但是如果不在同一个网站上进行此操作…

基于mimeTex的数学公式Webservice的部署和实现

通过Latex语法&#xff0c;实现生成数学公式的解决方案也很多。这里介绍一种方法&#xff0c;使用开源的mimeTex。该项目的官网地址如下&#xff1a;http://www.forkosh.com/mimetex.html网站主页有一个声明。如果你的服务器上已经安装了latex&#xff0c;那么推荐使用mathTex&…

对称加密算法AES之GCM模式简介及在OpenSSL中使用举例

AES(Advanced Encryption Standard)即高级加密标准&#xff0c;由美国国家标准和技术协会(NIST)于2000年公布&#xff0c;它是一种对称加密算法。关于AES的更多介绍可以参考&#xff1a;https://blog.csdn.net/fengbingchun/article/details/100139524 AES的GCM(Galois/Counte…

iOS UITextField清空按钮

extField.clearButtonModeUITextFieldViewModeWhileEditing; 就可以了&#xff0c;表明编辑输入框的时候启动一键清空按钮。另外&#xff0c;clearButtonMode还有三个属性&#xff1a; UITextFieldViewModeNever, 清空按钮永不出现 UITextFieldViewModeUnlessEditing, 不编…

腾讯“疯狂”开源

作者 | 马超责编 | 胡巍巍出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;近日&#xff0c;腾讯自研的万亿级分布式消息中间件TubeMQ正式开源&#xff0c;并捐赠给Apache基金会&#xff0c;成为基金会官方认可的Incubator项目。我们知道与TubeMQ功能类似的kafka是领…

[Android]开发摇一摇分歧表决器过程

心血来潮&#xff0c;走进Android&#xff0c;准备开发一个摇一摇分歧表决器&#xff08;PS&#xff1a;这个想法源自去年看的一个都市剧《约会专家》中主人公杭杭开发的分歧表决器APP&#xff09;。简述&#xff1a;摇一摇分歧表决器是一款Android App&#xff0c;通过将传统的…