公共PopDatePickerView实现

17 篇文章 0 订阅
13 篇文章 0 订阅

使用方法:

- (void) btnClicked:(id)sender {

    // 适应ios6ios7

    

    if (popDatePickerView == nil) {

        if (IS_IOS7) {

            popDatePickerView = [[PopDatePieckerView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height, 320, 206)];

        }

        else {

            popDatePickerView = [[PopDatePieckerView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height, 320, 260)];

        }

    }

    

    popDatePickerView.backgroundColor = [UIColor clearColor];

    popDatePickerView.delegate = self;

    

    // 设置时间格式 如果不设置则显示默认

    [popDatePickerView setDatePickerMode:UIDatePickerModeDateAndTime];

    

    // 设置时间显示的格式 如果不设置则显示默认

    [popDatePickerView setDateFormatter:[NSString stringWithFormat:@"%@", @"yyyy-MM-dd HH:mm"]];

    

    // 设置时间是否中文显示 如果不设置则显示默认

    local = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];

    [popDatePickerView setDateLocal:local];

    

    [self.view addSubview:popDatePickerView];

    

    [self startAnimation];

}


- (void) doneBarButtonItemClicked:(NSString *)strDate {

    

    [self stopAnimation];

}


#pragma mark 简单动画


- (void) startAnimation {

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3];

    [UIView setAnimationDelegate:popDatePickerView];

    

    if (IS_IOS7) {

        popDatePickerView.frame = CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height - 206, 320, 206);

    }

    else {

        popDatePickerView.frame = CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height - 260, 320, 260);

    }

    

    [UIView commitAnimations];

}


- (void) stopAnimation {

    [UIView animateWithDuration:0.3f animations:^{

        if (IS_IOS7) {

            popDatePickerView.frame = CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height + 20, 320, 206);

        }

        else {

            popDatePickerView.frame = CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height, 320, 260);

        }

    } completion:^(BOOL finished) {

    }];

}

PopDatePickerView.h

#import <UIKit/UIKit.h>



#define IS_IOS7 ([[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue] >= 7)


@protocol PopDatePickerViewDelegate <NSObject>


@optional

- (void) doneBarButtonItemClicked:(NSString *)strDate;


@end


@interface PopDatePieckerView : UIView


@property (nonatomic, weak) id <PopDatePickerViewDelegate> delegate;


@property (nonatomic, retain) UIToolbar *toolBar;

@property (nonatomic, retain) UIDatePicker *datePicker;

@property (nonatomic, retain) NSLocale *locale;

@property (nonatomic, retain) NSString *dateFormatter;


- (void) setDatePickerMode:(UIDatePickerMode)dateMode;


- (void) setDateFormatter:(NSString *)strFormatter;


- (void) setDateLocal:(NSLocale *)local;


@end



PopDatePickerView.m

#import "PopDatePieckerView.h"


#define kToolBarHeight 44.0


@implementation PopDatePieckerView {

    NSDateFormatter *dateFormatter;

}


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        

        self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, kToolBarHeight)];

        self.toolBar.backgroundColor = [UIColor clearColor];

        [self addSubview:self.toolBar];

        

        UIBarButtonItem *doneBarBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];

        UIBarButtonItem *sepBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

        NSArray *aryItesm = [[NSArray alloc] initWithObjects:sepBarItem, doneBarBtnItem, nil];

        [self.toolBar setItems:aryItesm animated:YES];

        

        self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, kToolBarHeight, self.frame.size.width, self.frame.size.height - self.toolBar.frame.origin.y - self.toolBar.frame.size.height)];

        self.datePicker.backgroundColor = [UIColor whiteColor];

        self.datePicker.datePickerMode = UIDatePickerModeDate;

        [self addSubview:self.datePicker];

        

        dateFormatter = [[NSDateFormatter alloc] init];

        [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    }

    return self;

}


- (void) setDatePickerMode:(UIDatePickerMode)dateMode {

    [self.datePicker setDatePickerMode:dateMode];

}


- (void) setDateFormatter:(NSString *)strFormatter {

    [dateFormatter setDateFormat:strFormatter];

}


- (void) setDateLocal:(NSLocale *)local {

    [self.datePicker setLocale:local];

}


- (void) doneButtonPressed:(id)sender {

    if (self.delegate) {

        NSString *strDate = [dateFormatter stringFromDate:self.datePicker.date];

        

        [self.delegate doneBarButtonItemClicked:strDate];

    }

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值