封装UIDatePicker,日期选择器

原创 2016年08月30日 21:40:23

在做日期选择中经常会用到UIDatePicker这个控件,在此简单封装了下
.h文件中

import

import “RMDateView.h”

CGFloat const kButtonHeightInDateView = 30;
CGFloat const kButtonwidthInDateView = 50;
CGFloat const kPickViewBackViewInDateView = 300;
@interface RMDateView ()
{
UIView *_backView;
}
@property (nonatomic, strong)UIDatePicker *datePicker;
@end
@implementation RMDateView

  • (instancetype)init
    {
    return [self initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) datePickerMode:UIDatePickerModeDate];
    }

  • (instancetype)initWithFrame:(CGRect)frame
    datePickerMode:(UIDatePickerMode)datePickerMode
    {
    self = [super initWithFrame:frame];
    if (self) {
    self.backgroundColor = [UIColor colorWithWhite:0.93 alpha:0.5];
    [self createViewWithdatePickerMode:datePickerMode];
    }
    return self;
    }

  • (void)createViewWithdatePickerMode:(UIDatePickerMode)datePickerMode
    {

    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kButtonHeightInDateView)];
    _backView.backgroundColor = [UIColor colorWithWhite:0.904 alpha:1.000];
    [self addSubview:_backView];

    // 取消
    UIButton *cancelButton = [RMUtils buttonWith:CGRectMake(0, 0, kButtonwidthInDateView, kButtonHeightInDateView) text:@”取消” backColor:[UIColor clearColor] textColor:RGB(100, 100, 100) tag:RMDateViewTagCancel];

    [cancelButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:cancelButton];

    // 确定
    UIButton *confirmButton = [RMUtils buttonWith:CGRectMake(kScreenWidth - kButtonwidthInDateView,0, kButtonwidthInDateView, kButtonHeightInDateView) text:@”确定” backColor:[UIColor clearColor] textColor:RGB(100, 100, 100) tag:RMDateViewTagCofirm];
    [confirmButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:confirmButton];

    self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_backView.frame), kScreenWidth, 216)];
    self.datePicker.backgroundColor = [UIColor whiteColor];
    self.datePicker.datePickerMode = datePickerMode;
    [self addSubview:self.datePicker];

    [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self addGestureRecognizer:tapGestureRecognizer];
    }

  • (void)setMaximumDate:(NSDate *)maximumDate
    {
    _maximumDate = maximumDate;
    self.datePicker.maximumDate = maximumDate;
    }

  • (void)buttonAction:(UIButton *)button
    {
    if ([_delegate respondsToSelector:@selector(RMDateView:dateViewTag:)]) {
    [self.delegate RMDateView:self dateViewTag:button.tag];
    }
    }

  • (void)tapGesture:(UIGestureRecognizer *)tap
    {
    [self endAnimationComplete:^{
    [self removeFromSuperview];
    }];
    }

  • (void)dateChanged:(UIDatePicker *)datePicker
    {
    if ([_delegate respondsToSelector:@selector(RMDateView:datePicker:)]) {
    [self.delegate RMDateView:self datePicker:datePicker];
    }

}

  • (void)startAnimation
    {
    [UIView animateWithDuration:0.3 animations:^{
    _backView.y = kScreenHeight - kPickViewBackViewInDateView;
    _datePicker.y = CGRectGetMaxY(_backView.frame);
    }];
    }

  • (void)endAnimationComplete:(void (^)())complete
    {
    [UIView animateWithDuration:0.3 animations:^{
    _backView.y = kScreenHeight;
    _datePicker.y = CGRectGetMaxY(_backView.frame);
    } completion:^(BOOL finished) {
    complete();
    }];
    }
    @end

简单的封了下,记录下,也便于下次更改,查阅
调用方法(可以直接类似正常的view一样直接add,也可以添加到window上,这里举添加window方式)
- (void)initDateView{
_dateView = [[RMDateView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)
datePickerMode:UIDatePickerModeDate];
_dateView.maximumDate = [NSDate date];
_dateView.delegate = self;
[_showView addSubview:_dateView];
[_dateView startAnimation];
}

pragma mark - RMDateDelegate

  • (void)RMDateView:(RMDateView *)view dateViewTag:(NSInteger)tag
    {
    switch (tag) {
    case RMDateViewTagCofirm:
    RMLog(@”确定”);
    [_dateView endAnimationComplete:^{
    [_dateView removeFromSuperview];
    }];
    break;
    case RMDateViewTagCancel:
    {
    RMLog(@”取消”);
    [_dateView endAnimationComplete:^{
    [_dateView removeFromSuperview];
    }];
    }
    break;
    default:
    break;
    }
    }

直接封装成了静态库,并删去了一下不必要操作
下载地址:
http://download.csdn.net/detail/sinat_28585351/9624078

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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

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

iOS日期选择器(UIDatePicker)

UIDatePicker是一个可以用来选择日期和时间的控件。除此之外,它也可作为倒计时控件。 日期选择器(UIDatePicker)继承了UIControl,因此UIDatePicker可以作为...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

iOS自定义UIDatepicker日期选择器视图

由于项目需要,需要定制一个日期选择器,找了半天没找到合适的就自己写了个demo; 自定义UIDatePicker日期选择器视图 效果如下: 下面贴上相关代码: ViewController: ...

swift笔记-日期选择器(UIDatePicker)

项目概述: 1,初始场景包含一个输出标签及一个工具栏,其中输出标签用于显示日期计算结果,工具栏包含一个按钮,用户点击触发到第二个场景。 2,第二个场景包含一个日期选择器,一个关闭视图按钮。 ...

UIDatePicker (时间、日期选择器使用)

#define k_datePicker_height 200 @interface ViewController (){ UIDatePicker *datePickerView;...

日期选择器UIDatePicker

在写日期选择器之前  我们必须准备几个成员变量(至少要下面几个)   UIToolbar *mToolBar;    //工具栏 UIDatePicker *mDatePickerView;  /...

代码实现日期选择器UIDatePicker使用

实现了一个中文显示的日期选择器,然后给其初始化一个年份信息

iOS 时间/ 日期选择器 UIDatePicker

Demo下载地址:http://download.csdn.net/detail/pyj111204/9559569 这里简单的写了一下 时间选择器 (UIDatePicker) datePick...

iOS增强版的UIDatePicker,省 市 县三级联动地址选择器(ActionSheetCustomPicker)

一般的项目中都会有一个选择地址的需求,系统的UIDatePicker肯定是不够的,找了个比较好用的库  ActionSheetCustomPicker 根据这个库,做了个简单使用的Demo githu...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)