自定義UIDatePicker,以後不用第三方的,想怎麼改就怎麼改~~~
基於上篇寫的自定義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
相關文章
- win10 資料夾圖示怎麼改_win10系統自定義圖示怎麼改Win10
- 美圖AI區域性重繪技術大揭秘!想怎麼改,就怎麼改!美圖區域性重繪讓你隨心所欲AI
- 學習Java以後的就業形勢怎麼樣?Java就業
- win10檔案圖示怎麼改_win10如何自定義圖示Win10
- echarts搞定各種地圖(想怎麼畫就怎麼畫)Echarts地圖
- CRM系統的介面怎麼改變?
- Mac郵件字型大小怎麼改Mac
- win10 怎麼改檔案字尾_w10系統怎麼改檔案字尾Win10
- 網站寶塔怎麼修改密碼?修改網站怎麼改?網站密碼
- 好用的CRM系統的介面怎麼改變?
- win10怎麼開啟自定義縮放 win10怎麼自定義縮放Win10
- Sketch怎麼自定義快捷鍵
- 華納雲:mysql怎麼自定義常量MySql
- 如何修改網站後臺的地址?公司網站被修改怎麼改?網站
- 怎麼改變spyder預設工作路徑
- Win10工作列顏色怎麼改?Win10
- PHP童鞋改JAVA程式碼怎麼處理PHPJava
- 路由器預設密碼怎麼改?路由器密碼
- 無線路由器怎麼改密碼 ?路由器密碼
- Swift 自定義 UIDatePickerSwiftUI
- win10怎麼更改字尾名_window10字尾怎麼改Win10
- jpeg轉化jpg怎麼操作 jpeg怎麼線上免費改為jpg格式
- Windows 8.1的“開始按鈕”應該怎麼改Windows
- 自定義模型怎麼在首頁呼叫?模型
- windows10qq飛車怎麼改幀數Windows
- jive-home改怎麼設定啊,暈死
- 公司網站預留電話修改怎麼改網站
- w10怎麼改ip地址_win10系統如何改IP地址Win10
- win10怎麼改microsoft名字 win10系統microsoft名字如何改Win10ROS
- win10中文改英文的步驟_w10系統怎麼中文改英文Win10
- 怎麼改變AIX上使用oracle的一些限制AIOracle
- 帝國cms生成的地址可以改嗎怎麼改回來
- 電腦系統win11改win10怎麼改?Win11改win10教程Win10
- 怎麼利用AbstractQueuedSynchronizer實現自定義同步元件?元件
- python怎麼自定義安裝路徑?Python
- 【日記】想見珍一面怎麼就這麼難(985 字)
- win10賬戶改密碼的方法_windows10賬戶密碼怎麼改Win10密碼Windows
- visio圓角矩形怎麼改變圓角大小