封装UIDatePicker,日期选择器

在做日期选择中经常会用到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

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_28585351/article/details/52373309
个人分类: iOS开发
上一篇商品添加购物车实现动画效果
下一篇iOS开发规范学习总结与框架搭建
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭