OC封装时间选择器

news/2024/7/7 19:33:25

#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







http://lihuaxi.xjx100.cn/news/235237.html

相关文章

蓝桥杯 扑克序列(全排列)

扑克序列 A A 2 2 3 3 4 4&#xff0c; 一共4对扑克牌。请你把它们排成一行。要求&#xff1a;两个A中间有1张牌&#xff0c;两个2之间有2张牌&#xff0c;两个3之间有3张牌&#xff0c;两个4之间有4张牌。 请填写出所有符合要求的排列中&#xff0c;字典序最小的那个。 例如&a…

runLoop和runtime的分析

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

iOS弹幕(源码)实现原理解析

弹幕&#xff0c;国内流行于视频网站A站和B站。网上关于弹幕的实现方法有很多&#xff0c;目前Android平台已经有比较成熟的解决方案DanmakuFlameMaster 。而iOS平台尚无比较成熟的开源库&#xff0c;在借鉴DanmakuFlameMaster的实现思想后&#xff0c;特分享iOS平台弹幕解决方…

MediaCodeC解码视频指定帧,迅捷、精确

原创文章&#xff0c;转载请联系作者 若待明朝风雨过&#xff0c;人在天涯&#xff01;春在天涯 原文地址 提要 最近在整理硬编码MediaCodec相关的学习笔记&#xff0c;以及代码文档&#xff0c;分享出来以供参考。本人水平有限&#xff0c;项目难免有思虑不当之处&#xff0c;…

Golang在视频直播平台的高性能实践(含PPT下载)

熊猫 TV 是一家视频直播平台&#xff0c;先介绍下我们系统运行的环境&#xff0c;下面这 6 大服务只是我们几十个服务中的一部分&#xff0c;由于并发量与重要性比较高&#xff0c;所以成为 golang 小试牛刀的首批高性能高并发服务。 把大服务拆细&#xff0c; 然后服务化独立部…

MAC设置允许任何来源

在升级了macOS Sierra (10.12)版本后在“安全性与隐私”中不再有“任何来源”选项 接下来&#xff0c;我们就打开终端&#xff0c;然后输入以下命令&#xff1a; sudo spctl --master-disable 输入后&#xff0c;可能会让你输入电脑的密码&#xff0c;输入就可以&#xff08;…

model多表操作

一. 创建模型 from django.db import models# Create your models here.#比较常用的作者信息放到这个表里面 class Author(models.Model): id models.AutoField(primary_keyTrue)namemodels.CharField(max_length32)agemodels.IntegerField()authorDetailmodels.OneToOneField…

SRS

SRS Overview SRS定位是运营级的互联网直播服务器集群&#xff0c;追求更好的概念完整性和最简单实现的代码。SRS提供了丰富的接入方案将RTMP流接入SRS&#xff0c;包括推送RTMP到SRS、推送RTSP/UDP/FLV到SRS、拉取流到SRS。SRS还支持将接入的RTMP流进行各种变换&#xff0c;譬…