You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
7.7 KiB
253 lines
7.7 KiB
![]()
2 years ago
|
//
|
||
|
// JGPicker.m
|
||
|
// JGPicker
|
||
|
//
|
||
|
// Created by stkcctv on 16/12/14.
|
||
|
// Copyright © 2016年 JG. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "JGPicker.h"
|
||
|
#import "Masonry.h"
|
||
|
|
||
|
#define ZERO 0
|
||
|
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
|
||
|
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
|
||
|
#define TOP_HEIGHGT 30
|
||
|
#define BUTTON_WIDTH 30
|
||
|
#define BUTTON_HEIGHT 30
|
||
|
|
||
|
#define DATAPICKER_HEIGHT 200
|
||
|
|
||
|
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
|
||
|
|
||
|
|
||
|
#define cusColor(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a] // 颜色
|
||
|
#define cusFont(font) [UIFont systemFontOfSize:font] // 字体
|
||
|
#define sureCellGrayColor cusColor(120, 120, 120, 1.0) // 灰色
|
||
|
#define circleNum 6 // 圆角的度数
|
||
|
#define screenWidthW [[UIScreen mainScreen] bounds].size.width
|
||
|
#define screenHeightH [[UIScreen mainScreen] bounds].size.height
|
||
|
|
||
|
|
||
|
#define headViewHHHHH 45 // 头视图的高度
|
||
|
#define datePickerHHHH 250 // pickView的高度
|
||
|
|
||
|
#define backButtonLeftDistance 0
|
||
|
#define backButtonW 60 // 取消和确定按钮的宽高
|
||
|
#define backButtonH 45
|
||
|
|
||
|
#define okButtonW 60
|
||
|
#define okButtonH 45
|
||
|
|
||
|
@implementation JGPicker
|
||
|
|
||
|
+(instancetype)datePickerViewWithType:(UIDatePickerMode)type andDelegate:(id)delegate;
|
||
|
{
|
||
|
JGPicker *picker = [[JGPicker alloc] initWithFrame:[UIScreen mainScreen].bounds type:type andDelegate:delegate];
|
||
|
|
||
|
return picker;
|
||
|
}
|
||
|
|
||
|
- (instancetype)initWithFrame:(CGRect)frame type:(UIDatePickerMode)type andDelegate:(id)delegate;
|
||
|
{
|
||
|
if (frame.size.width>frame.size.height)
|
||
|
{
|
||
|
float a = frame.size.height;
|
||
|
frame.size.height = frame.size.width;
|
||
|
frame.size.height = a;
|
||
|
}
|
||
|
self = [super initWithFrame:frame];
|
||
|
if (self)
|
||
|
{
|
||
|
self.type = type;
|
||
|
self.delegate = delegate;
|
||
|
[self addSubview:self.backgroundView];
|
||
|
[self initializationDatePicker];
|
||
|
[self initializationTopView];
|
||
|
[self initializationCancleButton];
|
||
|
[self initializationDetermineButton];
|
||
|
}
|
||
|
|
||
|
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
- (UIView *)backgroundView
|
||
|
{
|
||
|
// 不能用self.backgroundView 不然会循环引用
|
||
|
if (!_backgroundView)
|
||
|
{
|
||
|
self.backgroundView = [[UIView alloc] initWithFrame:self.frame];
|
||
|
CGFloat widthWithView=self.frame.size.width;
|
||
|
|
||
|
[self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]]; // 半透明
|
||
|
UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0,self.frame.size.height-(datePickerHHHH+headViewHHHHH),widthWithView,datePickerHHHH+headViewHHHHH)]; //蒙版视图的Frame
|
||
|
[bg setBackgroundColor:[UIColor whiteColor]];
|
||
|
bg.layer.cornerRadius=circleNum;
|
||
|
bg.layer.masksToBounds=YES;
|
||
|
[self addSubview:bg];
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
return _backgroundView;
|
||
|
}
|
||
|
|
||
|
//初始化TopView
|
||
|
- (void)initializationTopView
|
||
|
{
|
||
|
self.topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0,screenHeightH - datePickerHHHH - headViewHHHHH, screenWidthW,headViewHHHHH)];
|
||
|
self.topView.backgroundColor = [UIColor orangeColor];
|
||
|
[self.backgroundView addSubview:self.topView];
|
||
|
|
||
|
|
||
|
|
||
|
[self.topView setBackgroundColor:cusColor(178, 190,223,1.0)];
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
//初始化datePicker
|
||
|
- (void)initializationDatePicker
|
||
|
{
|
||
|
|
||
|
self.datePicker = [UIDatePicker new];
|
||
|
self.datePicker.datePickerMode = self.type;
|
||
|
self.datePicker.backgroundColor = [UIColor whiteColor];
|
||
|
self.datePicker.minimumDate = [NSDate date];
|
||
|
[self.datePicker addTarget:self action:@selector(datePickerChange:) forControlEvents:UIControlEventValueChanged];
|
||
|
[self.backgroundView addSubview:self.datePicker];
|
||
|
WS(weakSelf);
|
||
|
[_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.left.equalTo(weakSelf.backgroundView).with.offset(ZERO);
|
||
|
make.right.equalTo(weakSelf.backgroundView).with.offset(ZERO);
|
||
|
make.bottom.equalTo(weakSelf.backgroundView).with.offset(ZERO);
|
||
|
make.height.mas_equalTo(datePickerHHHH);
|
||
|
}];
|
||
|
|
||
|
}
|
||
|
|
||
|
//初始化button
|
||
|
- (void)initializationCancleButton
|
||
|
{
|
||
|
UIButton *backButton=[[UIButton alloc]initWithFrame:CGRectMake(backButtonLeftDistance,self.topView.frame.size.height/2-backButtonH/2, backButtonW, backButtonH)];
|
||
|
[backButton setTitle:@"取消" forState:UIControlStateNormal];
|
||
|
[backButton setTitle:@"取消" forState:UIControlStateHighlighted];
|
||
|
[backButton setTitleColor:sureCellGrayColor forState:UIControlStateNormal];
|
||
|
[backButton setTitleColor:sureCellGrayColor forState:UIControlStateHighlighted];
|
||
|
backButton.titleLabel.font=cusFont(15);
|
||
|
[backButton addTarget:self action:@selector(cancleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
|
||
|
[self.topView addSubview:backButton];
|
||
|
|
||
|
}
|
||
|
- (void)initializationDetermineButton
|
||
|
{
|
||
|
|
||
|
UIButton *OKButton=[[UIButton alloc]initWithFrame:CGRectMake(self.topView.frame.size.width-okButtonW,self.topView.frame.size.height/2-okButtonH/2, okButtonW, okButtonH)];
|
||
|
[OKButton setTitle:@"完成" forState:UIControlStateNormal];
|
||
|
[OKButton setTitle:@"完成" forState:UIControlStateHighlighted];
|
||
|
OKButton.titleLabel.font=cusFont(15);
|
||
|
[OKButton setTitleColor:cusColor(0, 97, 215, 1.0) forState:UIControlStateNormal];
|
||
|
[OKButton setTitleColor:cusColor(0, 97, 215, 1.0) forState:UIControlStateHighlighted];
|
||
|
[OKButton addTarget:self action:@selector(determineButtonClick:) forControlEvents:UIControlEventTouchUpInside];
|
||
|
[self.topView addSubview:OKButton];
|
||
|
|
||
|
}
|
||
|
|
||
|
- (void)show
|
||
|
{
|
||
|
// [UIView beginAnimations:nil context:nil];
|
||
|
// [UIView setAnimationCurve:2];
|
||
|
[[UIApplication sharedApplication].keyWindow addSubview:self];
|
||
|
// [UIView commitAnimations];
|
||
|
}
|
||
|
|
||
|
- (void)end
|
||
|
{
|
||
|
[self removeFromSuperview];
|
||
|
}
|
||
|
|
||
|
#pragma mark - DatePicker Method
|
||
|
- (void)datePickerChange:(UIDatePicker *)datePicker
|
||
|
{
|
||
|
// 判断delegate 指向的类是否实现协议方法
|
||
|
if ([self.delegate respondsToSelector:@selector(changeTime:)])
|
||
|
{
|
||
|
[_delegate changeTime:datePicker.date];
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
#pragma mark - buttonMethod
|
||
|
|
||
|
- (void)cancleButtonClick:(UIButton *)sender
|
||
|
{
|
||
|
[self removeFromSuperview];
|
||
|
}
|
||
|
|
||
|
- (void)determineButtonClick:(UIButton *)sender
|
||
|
{
|
||
|
if ([_delegate respondsToSelector:@selector(determinSelected:)])
|
||
|
{
|
||
|
[_delegate determinSelected:self.datePicker.date];
|
||
|
}
|
||
|
[self end];
|
||
|
}
|
||
|
|
||
|
|
||
|
- (NSDate*)dateFromString:(NSString*)dateString{
|
||
|
|
||
|
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
||
|
switch (self.type) {
|
||
|
case UIDatePickerModeTime:
|
||
|
[dateFormatter setDateFormat:@"HH:mm"];
|
||
|
break;
|
||
|
case UIDatePickerModeDate:
|
||
|
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
|
||
|
break;
|
||
|
case UIDatePickerModeDateAndTime:
|
||
|
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
|
||
|
break;
|
||
|
case UIDatePickerModeCountDownTimer:
|
||
|
[dateFormatter setDateFormat:@"HH:mm"];
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
NSDate *destDate= [dateFormatter dateFromString:dateString];
|
||
|
|
||
|
return destDate;
|
||
|
}
|
||
|
|
||
|
- (NSString*)stringFromDate:(NSDate*)date{
|
||
|
|
||
|
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
||
|
switch (self.type) {
|
||
|
case UIDatePickerModeTime:
|
||
|
[dateFormatter setDateFormat:@"HH:mm"];
|
||
|
break;
|
||
|
case UIDatePickerModeDate:
|
||
|
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
|
||
|
break;
|
||
|
case UIDatePickerModeDateAndTime:
|
||
|
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
|
||
|
break;
|
||
|
case UIDatePickerModeCountDownTimer:
|
||
|
[dateFormatter setDateFormat:@"HH:mm"];
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
NSString *destDateString = [dateFormatter stringFromDate:date];
|
||
|
|
||
|
return destDateString;
|
||
|
|
||
|
}
|
||
|
|
||
|
@end
|