基于上篇写的自定义UIAlertView,今天写了一个自定义的UIDatePicker,有弹出动画效果,利用block回调监听按钮,用法很简单。代码注释也都写了,有什么问题,留言给我。
直接上代码
用法如下:
SPDatePickerView *datePickerView = [[SPDatePickerView alloc]initWithTitle:@"选择出生日期" datePickerMode:UIDatePickerModeDate selectedDate:nil minimumDate:nil maximumDate:nil cancleBlock:^{
DDLog(@"取消");
}doneBlock:^(NSDate *date){
DDLog(@"确定 %@",date);
}];
[datePickerView show];
SPDatePickerView.h
//
// SPDatePickerView.h
//
// Created by ZSP on 2017/6/2.
//
#import
typedef void (^CancleBlock)();
typedef void (^DoneBlocks)(NSDate *date);
@interface SPDatePickerView : UIView
@property (nonatomic, copy) CancleBlock cancleBlock;
@property (nonatomic, copy) DoneBlocks doneBlock;
/**
SPDatePickerView
@param title 中间的标题
@param datePickerMode 选择时间的类型
@param selectedDate 默认显示的时间点
@param minimumDate 最小时间点
@param maximumDate 最大时间点
@param cancleBlock 取消按钮的回调
@param doneBlock 确定按钮的回调
@return self
*/
- (instancetype)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate minimumDate:(NSDate *)minimumDate maximumDate:(NSDate *)maximumDate cancleBlock:(void(^)())cancleBlock doneBlock:(void(^)(NSDate *date))doneBlock;
/**
显示SPDatePickerView展示
*/
- (void)show;
@end
SPDatePickerView.m
//
// SPDatePickerView.m
//
// Created by ZSP on 2017/6/1.
//
#import "SPDatePickerView.h"
@interface SPDatePickerView()
@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UIView *backView;
@end
@implementation SPDatePickerView
- (instancetype)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate minimumDate:(NSDate *)minimumDate maximumDate:(NSDate *)maximumDate cancleBlock:(void(^)())cancleBlock doneBlock:(void(^)(NSDate *date))doneBlock
{
self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
if (self) {
[self addSubViewsWithTitle:title datePickerMode:datePickerMode selectedDate:selectedDate minimumDate:minimumDate maximumDate:maximumDate cancleBlock:^{
cancleBlock();
} doneBlock:^(NSDate *date) {
doneBlock(date);
}];
}
return self;
}
- (void)addSubViewsWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate minimumDate:(NSDate *)minimumDate maximumDate:(NSDate *)maximumDate cancleBlock:(void(^)())cancleBlock doneBlock:(void(^)(NSDate *date))doneBlock
{
self.cancleBlock = [cancleBlock copy];
self.doneBlock = [doneBlock copy];
// self.backgroundColor = mRGBAColor(0, 0, 0, 0.3);
self.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
_backView = [[UIView alloc]init];
[self addSubview:_backView];
_backView.backgroundColor = [UIColor whiteColor];
[_backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self);
make.height.equalTo(@250);
}];
UIView *toolBarview = [[UIView alloc]init];
toolBarview.backgroundColor = [UIColor colorWithRed:43/255.0 green:189/255.0 blue: 152/255.0 alpha:1.0];
[_backView addSubview:toolBarview];
[toolBarview mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.equalTo(_backView);
make.height.equalTo(@50);
}];
//取消和确认按钮
UIButton *cancleButton = [[UIButton alloc]init];
cancleButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
cancleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[cancleButton setTitle:@"取消" forState:UIControlStateNormal];
cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
[cancleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[toolBarview addSubview:cancleButton];
[cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.equalTo(toolBarview);
make.width.equalTo(@(SCREEN_WIDTH/4));
}];
[cancleButton addTarget:self action:@selector(cancleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *doneButton = [[UIButton alloc]init];
doneButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
doneButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
[doneButton setTitle:@"确定" forState:UIControlStateNormal];
doneButton.titleLabel.font = [UIFont systemFontOfSize:14];
[doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[toolBarview addSubview:doneButton];
[doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.top.bottom.equalTo(toolBarview);
make.width.equalTo(@(SCREEN_WIDTH/4));
}];
[doneButton addTarget:self action:@selector(doneButtonClick:) forControlEvents:UIControlEventTouchUpInside];
if (title) {
UILabel *titleLable = [[UILabel alloc]init];
titleLable.textAlignment = NSTextAlignmentCenter;
[toolBarview addSubview:titleLable];
titleLable.font = [UIFont systemFontOfSize:11];
titleLable.textColor = [UIColor whiteColor];
titleLable.text = title;
[titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.equalTo(toolBarview);
make.left.equalTo(cancleButton.mas_right);
make.right.equalTo(doneButton.mas_left);
}];
}
_datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-260, SCREEN_WIDTH, 260)];
_datePicker.backgroundColor = [UIColor whiteColor];
// 设置时区
[_datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];
_datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
// 设置UIDatePicker的显示模式
[_datePicker setDatePickerMode:datePickerMode];
// 设置显示最大,最小时间
if (minimumDate) {
[_datePicker setMinimumDate:minimumDate];
}
if (maximumDate) {
[_datePicker setMaximumDate:maximumDate];
}
// 设置当前显示时间
if (selectedDate) {
[_datePicker setDate:selectedDate];
}
[_backView addSubview:_datePicker];
[_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.equalTo(self);
make.top.equalTo(toolBarview.mas_bottom);
}];
[self animationWithView:_backView duration:0.5];
}
- (void)cancleButtonClick:(UIButton *)sender{
[self animationWithViewDisappear];
if (self.cancleBlock) {
self.cancleBlock();
}
}
- (void)doneButtonClick:(UIButton *)sender{
[self animationWithViewDisappear];
if (self.doneBlock) {
self.doneBlock(self.datePicker.date);
}
}
#pragma mark -----CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
[self removeFromSuperview];
DDLog(@"动画结束");
}
//从上往下移动
- (void)animationWithViewDisappear{
[_backView.superview layoutIfNeeded];
[_backView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.top.equalTo(self.mas_bottom).offset(10);
make.height.equalTo(@250);
}];
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 0.4f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
animation.type = kCATransitionReveal;
// animation.removedOnCompletion = NO;
animation.subtype = kCATransitionFromBottom;
[_backView.layer addAnimation:animation forKey:@"animation"];
}
//从下往上移动
- (void)animationWithView:(UIView *)view duration:(CFTimeInterval)duration{
CATransition *animation = [CATransition animation];
//animation.delegate = self;
animation.duration = duration;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
animation.type = kCATransitionMoveIn;
animation.subtype = kCATransitionFromTop;
[view.layer addAnimation:animation forKey:@"animation"];
}
- (void)show{
[[UIApplication sharedApplication].keyWindow addSubview:self];
}
@end