自定义UIDatePicker,以后不用第三方的,想怎么改就怎么改~~~

SPDatePickerView效果图

基于上篇写的自定义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 <UIKit/UIKit.h>

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()<CAAnimationDelegate>

@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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,535评论 4 61
  • 大家好,我是日记星球116号星宝宝李菊梅,大家都叫我梅姐。即将参加3月份第三期日记星球21天蜕变之旅,这是我...
    卖货不求人找梅姐阅读 617评论 0 0
  • 周五晚上看了《北京遇上西雅图之不二情书》,第一部我是在影院看的,感觉不错,惊艳于汤唯的气质和英语,也喜欢儒雅的吴秀...
    龙凝阅读 698评论 0 1
  • 走过山、大河 路过你的梦 山一程,水一程 留下岁月的伤痕 流水呵 我是枫雨林的未亡人 火光灼伤了我的双臂啊 是你冰...
    墨汐梓沫阅读 349评论 0 0
  • 下班后,整个人像被抽空一般,回到家打开文献,赶点进度! 今天修改的文章还剩下最后一小部分,明天早上可以完成,并做一...
    宬睿阅读 228评论 0 0

友情链接更多精彩内容