关闭

iOS开发 选择日期的view 一(UIDatePicker的封装)

标签: UIDatePicker封装
2132人阅读 评论(0) 收藏 举报
分类:

之前写了个基于类簇的自定义选择日期的view的封装,现在看看,感觉代码挺操蛋的,还是抽出来,只显示UIDatePicker的封装吧

//
//  XGChoseDateView.h
//  XGDevelopDemo
//
//  Created by 小广 on 15/8/18.
//  Copyright © 2015年 小广. All rights reserved.
//  选择日期的view

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, DateType) {
    DateTypeNormal,       // 默认时间选择(月日时分)
    DateTypeModeDate      // 时间选择(只有年月日)
};

typedef void (^ConfirmDateBlock)(NSDate  *date);

@interface XGChoseDateView : UIView

- (instancetype)initWithFrame:(CGRect)frame
               datePickerMode:(UIDatePickerMode)datePickerMode
                     lastDate:(NSDate *)lastDate;

/**
 *  View显示
 */
- (void)showView;

/**
 *  确认所选时间
 *
 *  @param block 传出Date
 */
- (void)confirmDate:(ConfirmDateBlock)block;

@end

.m里面的 代码 有点多,也懒得优化,都是用的纯代码

//
//  XGChoseDateView.m
//  XGDevelopDemo
//
//  Created by 小广 on 15/8/18.
//  Copyright © 2015年 小广. All rights reserved.
//  选择日期的view

#import "XGChoseDateView.h"

#define XGButtonWidth         60.0
#define TopBarHeight          44.0
#define PickerHeight          216.0
#define XGScreenBounds [UIScreen mainScreen].bounds
#define XGScreenWidth XGScreenBounds.size.width
#define XGScreenHeight XGScreenBounds.size.height
#define XGViewHeight CGRectGetHeight(XGScreenBounds) - 44 - 20 // 去掉导航条的高度

@interface XGChoseDateView ()

@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UIView       *bgView;        // 按钮背景View
@property (nonatomic, strong) UIButton     *cancelButton;  // 取消按钮
@property (nonatomic, strong) UIButton     *confirmButton; // 确定按钮
@property (nonatomic, strong) NSDate       *currentDate;   // 当前显示时间
@property (nonatomic, strong) NSDate       *lastDate;      // 上次选择时间
@property (nonatomic, assign) UIDatePickerMode datePickerMode; // datePicker显示形式
@property (nonatomic, copy) ConfirmDateBlock block;

@end

@implementation XGChoseDateView

#pragma mark - 对外方法
- (instancetype)initWithFrame:(CGRect)frame
               datePickerMode:(UIDatePickerMode)datePickerMode
                     lastDate:(NSDate *)lastDate {
    self = [super initWithFrame:frame];
    if (self) {
        _datePickerMode = datePickerMode;
        _lastDate = lastDate;
        [self initSubViews];
    }
    return self;
}

// 显示view(此方法是加载在window上 ,遮住导航条)
- (void)showView {
    UIWindow * window = [UIApplication sharedApplication].windows[0];
    [window addSubview:self];
}

// 传值到外面
- (void)confirmDate:(ConfirmDateBlock)block {
    self.block = block;
}

#pragma mark - 自定义方法
- (void)initSubViews {
    self.backgroundColor = [UIColor clearColor];
    self.alpha = 0;
    [self addCoverView];
    [self addBGView];
    self.datePicker.datePickerMode = self.datePickerMode;
    self.datePicker.date = self.lastDate ? self.lastDate : [NSDate date];
    self.currentDate = self.datePicker.date;
}

// 添加蒙板
- (void)addCoverView {
    UIView *coverView = [[UIView alloc] initWithFrame:self.frame];
    coverView.alpha = 0.3;
    coverView.backgroundColor = [UIColor blackColor];
    [self addSubview:coverView];
    
    UITapGestureRecognizer  *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverViewDidTouch:)];
    tapGesture.numberOfTapsRequired = 1;
    [coverView addGestureRecognizer:tapGesture];
}

// 大view添加子view(为了做动画)
- (void)addBGView {
    [self addSubview:self.bgView];
    [self addButtons];
    [self.bgView addSubview:self.datePicker];
    [self.datePicker addTarget:self action:@selector(datePickerValueChange:) forControlEvents:UIControlEventValueChanged];
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 1.0;
        CGFloat posY = XGScreenHeight - (PickerHeight + TopBarHeight);
        self.bgView.frame = CGRectMake(0.0, posY, XGScreenWidth, (PickerHeight + TopBarHeight));
    } completion:^(BOOL finished) {
        //
    }];
}

// 添加按钮
- (void)addButtons {
    
    CGFloat posY = 0.0;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, posY, XGScreenWidth, TopBarHeight)];
    view.backgroundColor = UIColorFrom16RGB(0x334455);
    [self.bgView addSubview:view];
    
    // 取消按钮
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelButton.frame = CGRectMake(0.0, posY, XGButtonWidth, TopBarHeight);
    self.cancelButton.tag = 0;
    [self.cancelButton setBackgroundColor:[UIColor clearColor]];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.cancelButton];
    
    // 确定按钮
    CGFloat posX = XGScreenWidth - XGButtonWidth;
    self.confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.confirmButton.frame = CGRectMake(posX, posY, XGButtonWidth, TopBarHeight);
    self.confirmButton.tag = 1;
    [self.confirmButton setBackgroundColor:[UIColor clearColor]];
    [self.confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.confirmButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.confirmButton];
    
}

// 按钮点击事件
- (void)buttonClick:(UIButton *)sender {
    if (sender.tag == 1) {
        if (!self.currentDate) {
            self.currentDate = [NSDate date];
        }
        if (self.block) {
            self.block(self.currentDate);
        }
    }
    
    [self dismissContactView];
}

// 蒙板手势事件
- (void)coverViewDidTouch:(UITapGestureRecognizer *)sender {
    if (!self.currentDate) {
        self.currentDate = [NSDate date];
    }
    if (self.block) {
        self.block(self.currentDate);
    }
    [self dismissContactView];
}


// 移除view
- (void)dismissContactView {
    kWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.bgView.frame = CGRectMake(0.0, XGScreenHeight, XGScreenWidth, 0.0);
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - 懒加载
// PickerView和按钮的父视图view(为了做动画)
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, XGScreenHeight, XGScreenWidth, (PickerHeight + TopBarHeight))];
        _bgView.backgroundColor = [UIColor clearColor];
        _bgView.clipsToBounds = YES;
    }
    return _bgView;
}

- (UIDatePicker *)datePicker {
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, TopBarHeight, XGScreenWidth, 216.0)];
        _datePicker.backgroundColor = [UIColor whiteColor];
    }
    return _datePicker;
}

#pragma mark - 代理方法
// UIDatePicker绑定方法
- (void)datePickerValueChange:(UIDatePicker *)sender {
    
    self.currentDate = sender.date;
}

@end

用法:

XGChoseDateView *choseView = [[XGChoseDateView alloc] initWithFrame:XGScreenBounds
                                                         datePickerMode:UIDatePickerModeDate
                                                               lastDate:self.choseDate];
    [choseView showView];
    __weak __typeof(self)weakSelf = self;
    [choseView confirmDate:^(NSDate *date) {
        weakSelf.choseDate = date;
        NSLog(@"当前选择的时间是==%@==",date);
    }];

其中的choseDate 也就是记录上次所选择的时间,就酱,还有很多待优化,以后有时间了再说...

效果:


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

IOS基础UI之(十) UIPickerView和UIDatePicker详解

UIPickerView和UIDatePicker使用起来相对比较简单,下面通过简单例子深入掌握它们。 UIPickerView 1.UIPickerView 属性 // 数据源(用来告诉...
  • zhixinhuacom
  • zhixinhuacom
  • 2015-11-04 22:44
  • 2353

iOS 自定义UIDatePicker

自定义UIPickerView方法,定义一个自己的View,然后把uipickerView 和其他的一些组件添加到View中去 如图所示: 代码: @protocol CustomerDatePi...
  • u013087068
  • u013087068
  • 2015-03-21 10:21
  • 608

ios开发 自定义日期选择器 PTXDatePickerView

可能有一部分同学在项目中需要用到一个时间选择器,又不想自己去自定义,那么在这里分享一个自定义的时间选择器。可能直接用在你的项目中不合适,但可以给你提供参考,并快速自定义出一个时间选择器。 首先描述下该...
  • mrtianxiang
  • mrtianxiang
  • 2017-01-16 10:24
  • 990

UIDatePicker 日期/时间选取器(滚轮)—IOS开发

声明 欢迎转载,但是请尊重作者劳动成果,转载请保留此框内声明,谢谢。 文章出处:http://blog.csdn.net/iukey UIDatePi...
  • iukey
  • iukey
  • 2012-03-07 00:58
  • 90306

安卓开发-高仿ios时间选择控件timepicker

在开发中如果有地址或者日期选择等就会涉及到时间或者条件选择器,大家都会想到仿ios的三级联动的效果,用wheelview实现,其实安卓原生自带了时间和日期选择器可能是效果来说太粗犷了,所以很多产品效果...
  • panghaha12138
  • panghaha12138
  • 2017-06-20 14:38
  • 2842

UIDatePicker 日期/时间选取器(滚轮)—IOS开发

UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期、时间和持续时长的输入。日期选取器的各列会按照指定的风格进行自动配置,这样...
  • super_man_ww
  • super_man_ww
  • 2016-10-11 10:43
  • 1060

iOS开发UItextfile弹出日期选择控制器输入日期

UItextFile和UIDatePicker进行相互结合
  • u011418878
  • u011418878
  • 2016-03-02 10:36
  • 569

iOS 开发时间选择器datePicker

-(void)timeSearch{ //time search self.dateView = [[UIView alloc]initWithFrame:CGRectMake(0...
  • Cai_guange1226
  • Cai_guange1226
  • 2016-03-04 11:05
  • 4371

iOS开发(OC)——日期选择器

可以选择年月日时分 新建继承UIView的新类DatePickerView DatePickerView.h代码#import typedef void(^Select)(int stag);//...
  • liumude123
  • liumude123
  • 2016-06-04 21:07
  • 200

Swift之UIDatePicker

UIDatePicker 是UIKit控件中提供日期和时间选择的控件。首先我们先了解一下相关属性 1、Mode 控件的模式设置,有如下几种模式:  A、Date:选择这种模式,控件仅显示日期,不显...
  • NoPolun_iOS
  • NoPolun_iOS
  • 2017-01-07 21:38
  • 377
    个人资料
    • 访问:205247次
    • 积分:2892
    • 等级:
    • 排名:第14187名
    • 原创:86篇
    • 转载:5篇
    • 译文:0篇
    • 评论:61条
    文章分类
    最新评论