自定義UIDatePicker,以後不用第三方的,想怎麼改就怎麼改~~~

weixin_34391445發表於2017-06-02
1715517-0aaa7977dac2bd41.gif
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

相關文章