iOS开发——自定义的时间选择器

项目遇到要用时间选择器,但是系统的有时候往往不能满足需求,这时候就需要我们自定义去实现,今天就把项目中自己封装的datapicker分享给大家,多多指教。

#import 

@protocol WQDatePickerDelegate 

-(void)dateSelected:(NSDate *)date actionIndex:(NSInteger )index;

-(void)animationFinished;

@end

@interface WQDatePickerView : UIView

@property(nonatomic,assign)NSDate *date;
@property(nonatomic,weak) id delegate;

-(instancetype)initDatePicker:(CGRect) rect defaultDate:(NSDate *) defaultDate selectFutureDate:(BOOL)select;

-(void)animationFinished;

-(void)show;

-(void)dismiss;

@end

 

#import "WQDatePickerView.h"
#import "Header.h"
#import 

static const int HEIGHT = 200;
@interface WQDatePickerView()
{
    UIDatePicker *_datePicker;
    BOOL _canSelectFutrueDate;
}

@end

@implementation WQDatePickerView


-(instancetype)initDatePicker:(CGRect)rect defaultDate:(NSDate *)defaultDate selectFutureDate:(BOOL)select{
    if (self = [super initWithFrame:rect]) {
        _date = defaultDate;
        _canSelectFutrueDate = select;
        [self initView];
    }
    return self;
}

-(void)initView{
    NSMutableArray *buttons = [[NSMutableArray alloc]initWithCapacity:2];
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth, AutoLayoutUIScreenHeight(40))];
    [toolbar setBackgroundColor:UIColorFromRGB(0xfbfbfb)];
    
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(toolbarAction:)];
    cancelItem.tag = 1;
    
    UIBarButtonItem *flexibleSpaceItem =[[UIBarButtonItem alloc]
                         initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                         target:self
                         action:NULL];

    
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(toolbarAction:)];
    doneItem.tag = 2;
    
    [buttons addObject:cancelItem];
    [buttons addObject:flexibleSpaceItem];
    [buttons addObject:doneItem];
    [toolbar setItems:buttons animated:YES];
    [self addSubview:toolbar];
    
    
    _datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, AutoLayoutUIScreenHeight(40), ScreenWidth, HEIGHT-40)];
    if (!_date) {
        _date = [NSDate date];
    }
    [_datePicker setDate:_date];
    [_datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    [_datePicker setDatePickerMode:UIDatePickerModeDate];
    
    if (!_canSelectFutrueDate) {
        [_datePicker setMaximumDate:[NSDate date]];
    }
    
    [self addSubview:_datePicker];
 
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0.0f, -3.0f);
    self.layer.shadowOpacity = 0.2f;
}

-(void)toolbarAction:(UIBarButtonItem *)item{
    NSInteger tag = item.tag;
    NSLog(@"action");
    if ([_delegate respondsToSelector:@selector(dateSelected:actionIndex:)]) {
        _date = [_datePicker date];
        [_delegate dateSelected:[_datePicker date] actionIndex:tag];
    }
}

- (void)show{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];//动画时间长度,单位秒,浮点数
    [self exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    self.frame = CGRectMake(0, ScreenHeight-HEIGHT, ScreenWidth, HEIGHT);
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}

- (void)dismiss {
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];//动画时间长度,单位秒,浮点数
    self.frame = CGRectMake(0, ScreenHeight, ScreenWidth, HEIGHT);
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationFinished)];
    [UIView commitAnimations];
}

-(void)animationFinished{
    if ([_delegate respondsToSelector:@selector(animationFinished)]) {
        [_delegate animationFinished];
    }
}

 

转载于:https://www.cnblogs.com/qinxiaoguang/p/5556120.html

你可能感兴趣的:(iOS开发——自定义的时间选择器)