自定义UIPickerView方法,定义一个自己的View,然后把uipickerView 和其他的一些组件添加到View中去
如图所示:
代码:
@protocol CustomerDatePickerDelegate <NSObject> //声明一个代理
-(void) selectDateWithString:(NSString *)date;
@end
@interface CustomerDatePickerView :UIView
@property(strong,nonatomic) UIDatePicker *datePicker;
@property(strong,nonatomic) id<CustomerDatePickerDelegate> delegate;
-(void) show; //显示
-(void) close; //消失
@end
//
// CustomerDatePickerView.m
// practice
//
// Created by user on 15/3/20.
// Copyright (c) 2015年 user. All rights reserved.
//
#import "CustomerDatePickerView.h"
@implementation CustomerDatePickerView
#define AnimationDuration 0.3
-(NSDateFormatter *)getDateFormater
{
staticNSDateFormatter *format;
staticdispatch_once_t once;
dispatch_once(&once, ^{
format = [[NSDateFormatteralloc]init];
[format setTimeZone:[NSTimeZonetimeZoneWithAbbreviation:@"HKT"]];
});
return format;
}
-(instancetype) initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if(self){
self.backgroundColor = [UIColorwhiteColor];
_datePicker = [[UIDatePickeralloc]init];
_datePicker.datePickerMode =UIDatePickerModeDate;
[_datePickersetLocale:[[NSLocalealloc]initWithLocaleIdentifier:@"zh_CN"]];
_datePicker.frame = CGRectMake(0,44,self.frame.size.width,216);
[_datePickersetBackgroundColor:[UIColorwhiteColor]];
[selfaddSubview:_datePicker];
UIView *topView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,44)];
[selfaddSubview:topView];
UIButton *cancelButton = [[UIButtonalloc]initWithFrame:CGRectMake(30,8,40, 28)];
UIButton *confirmButton = [[UIButtonalloc]initWithFrame:CGRectMake(self.frame.size.width - 30 - 40,8,40, 28)];
[cancelButton setTitle:@"取消"forState:UIControlStateNormal];
[confirmButton setTitle:@"确定"forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColorgreenColor]forState:UIControlStateNormal];
[confirmButton setTitleColor:[UIColorgreenColor]forState:UIControlStateNormal];
[cancelButton addTarget:selfaction:@selector(cancelButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[confirmButton addTarget:selfaction:@selector(confirmButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:cancelButton];
[selfaddSubview:confirmButton];
}
return self;
}
-(void)confirmButtonClicked
{
if(self.delegate && [self.delegaterespondsToSelector:@selector(selectDateWithString:)])
{
NSDate *date = [_datePickerdate];
NSDateFormatter *foramte = [selfgetDateFormater];
[foramtesetDateFormat:@"yyyy-MM-dd"];
[self.delegateselectDateWithString:[foramtestringFromDate:date]];
}
[selfclose];
}
-(void) cancelButtonClicked
{
[selfclose];
}
-(void) showDataPickerView:(BOOL)isShow withAnimation:(NSTimeInterval)duration
{
@synchronized(self){
if(isShow){
[UIViewanimateWithDuration:durationanimations:^{
[selfsetFrame:CGRectMake(0, [UIScreenmainScreen].bounds.size.height -self.frame.size.height,self.frame.size.width,self.frame.size.width)];
}];
}else {
[UIViewanimateWithDuration:durationanimations:^{
[selfsetFrame:CGRectMake(0, [UIScreenmainScreen].bounds.size.height,self.frame.size.width,self.frame.size.height)];
}completion:^(BOOL finished){
}];
}
};
}
-(void) show
{
[selfshowDataPickerView:YESwithAnimation:AnimationDuration];
}
-(void) close
{
[selfshowDataPickerView:NOwithAnimation:AnimationDuration];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
@interface ViewController ()<CustomerDatePickerDelegate>
{
UIView *_backView;
UIView *_firstView;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(100,100,100, 30)];
[button setBackgroundColor:[UIColorgrayColor]];
[[NSUserDefaultsstandardUserDefaults]setObject:@"您好"forKey:@"title"];
[button setTitle:[[NSUserDefaults standardUserDefaults]objectForKey:@"title"]forState:UIControlStateNormal];
//[self.view addSubview:button];
if(button.isSelected ==YES)
{
}
[button addTarget:selfaction:@selector(changeView)forControlEvents:UIControlEventTouchUpInside];
_backView = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
[_backViewsetFrame:CGRectMake(-self.view.frame.size.width,0,self.view.frame.size.width,self.view.frame.size.height)];
[_backViewsetBackgroundColor:[UIColorgrayColor]];
_backView.layer.borderColor = [UIColorredColor].CGColor;
_backView.layer.borderWidth =2.0f;
[self.viewaddSubview:_backView];
[self.viewsendSubviewToBack:_backView];
_firstView = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
[self.viewaddSubview:_firstView];
[_firstViewsetBackgroundColor:[UIColorgreenColor]];
_firstView.layer.borderColor = [UIColororangeColor].CGColor;
_firstView.layer.borderWidth =2.0f;
[_firstViewaddSubview:button];
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapGesture:)];
[_firstViewaddGestureRecognizer:tap];
}
-(void) changeView
{
CustomerDatePickerView *pickerView = [[CustomerDatePickerViewalloc]initWithFrame:CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,260)];
pickerView.delegate =self;
[self.viewaddSubview:pickerView];
[pickerViewshow];
}
//实现代理函数
-(void) selectDateWithString:(NSString *)date
{
NSLog(@"%@",date);
}