iOS自定義日期picker選擇器,參考他人代碼...

類似於ios系統自帶的日期選擇器...


循環滑動類...

//
//  MXSCycleScrollView.h
//  xuexin
//  e-mail:[email protected]
//  Created by renbing on 3/7/14.
//  Copyright (c) 2014 renbing. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol MXSCycleScrollViewDelegate;
@protocol MXSCycleScrollViewDatasource;

@interface MXSCycleScrollView : UIView<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    
    NSInteger _totalPages;
    NSInteger _curPage;
    
    NSMutableArray *_curViews;
}

@property (nonatomic,readonly) UIScrollView *scrollView;
@property (nonatomic,assign) NSInteger currentPage;
@property (nonatomic,assign,setter = setDataource:) id<MXSCycleScrollViewDatasource> datasource;
@property (nonatomic,assign,setter = setDelegate:) id<MXSCycleScrollViewDelegate> delegate;

- (void)setCurrentSelectPage:(NSInteger)selectPage; //設置初始化頁數
- (void)reloadData;
- (void)setViewContent:(UIView *)view atIndex:(NSInteger)index;

@end

@protocol MXSCycleScrollViewDelegate <NSObject>

@optional
- (void)didClickPage:(MXSCycleScrollView *)csView atIndex:(NSInteger)index;
- (void)scrollviewDidChangeNumber:(MXSCycleScrollView *)csView;

@end

@protocol MXSCycleScrollViewDatasource <NSObject>

@required
- (NSInteger)numberOfPages:(MXSCycleScrollView*)scrollView;
- (UIView *)pageAtIndex:(NSInteger)index andScrollView:(MXSCycleScrollView*)scrollView;

@end

//
//  MXSCycleScrollView.m
//  xuexin
//  e-mail:[email protected]
//  Created by renbing on 3/7/14.
//  Copyright (c) 2014 renbing. All rights reserved.
//

#import "MXSCycleScrollView.h"

@implementation MXSCycleScrollView

@synthesize scrollView = _scrollView;
@synthesize currentPage = _curPage;
@synthesize datasource = _datasource;
@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(self.bounds.size.width, (self.bounds.size.height/5)*7);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.contentOffset = CGPointMake(0, (self.bounds.size.height/5));
        
        [self addSubview:_scrollView];
    }
    return self;
}
//設置初始化頁數
- (void)setCurrentSelectPage:(NSInteger)selectPage
{
    _curPage = selectPage;
}

- (void)setDataource:(id<MXSCycleScrollViewDatasource>)datasource
{
    _datasource = datasource;
    [self reloadData];
}

- (void)reloadData
{
    _totalPages = [_datasource numberOfPages:self];
    if (_totalPages == 0) {
        return;
    }
    [self loadData];
}

- (void)loadData
{
    //從scrollView上移除所有的subview
    NSArray *subViews = [_scrollView subviews];
    if([subViews count] != 0) {
        [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    [self getDisplayImagesWithCurpage:_curPage];
    
    for (int i = 0; i < 7; i++) {
        UIView *v = [_curViews objectAtIndex:i];
        v.frame = CGRectOffset(v.frame, 0, v.frame.size.height * i );
        [_scrollView addSubview:v];
    }
    
    [_scrollView setContentOffset:CGPointMake( 0, (self.bounds.size.height/5) )];
}

- (void)getDisplayImagesWithCurpage:(NSInteger)page {
    NSInteger pre1 = [self validPageValue:_curPage-1];
    NSInteger pre2 = [self validPageValue:_curPage];
    NSInteger pre3 = [self validPageValue:_curPage+1];
    NSInteger pre4 = [self validPageValue:_curPage+2];
    NSInteger pre5 = [self validPageValue:_curPage+3];
    NSInteger pre = [self validPageValue:_curPage+4];
    NSInteger last = [self validPageValue:_curPage+5];
    
    if (!_curViews) {
        _curViews = [[NSMutableArray alloc] init];
    }
    
    [_curViews removeAllObjects];
    
    UILabel *oneLabel = (UILabel*)[_datasource pageAtIndex:pre1 andScrollView:self];
    [oneLabel setFont:[UIFont systemFontOfSize:14]];
    [oneLabel setTextColor:RGBACOLOR(186.0, 186.0, 186.0, 1.0)];
    
    UILabel *twoLabel = (UILabel*)[_datasource pageAtIndex:pre2 andScrollView:self];
    [twoLabel setFont:[UIFont systemFontOfSize:14]];
    [twoLabel setTextColor:RGBACOLOR(186.0, 186.0, 186.0, 1.0)];
    
    UILabel *currentLabel = (UILabel*)[_datasource pageAtIndex:pre3 andScrollView:self];
    [currentLabel setFont:[UIFont systemFontOfSize:16]];
    [currentLabel setTextColor:RGBACOLOR(113.0, 113.0, 113.0, 1.0)];
    
    UILabel *threeLabel = (UILabel*)[_datasource pageAtIndex:pre4 andScrollView:self];
    [threeLabel setFont:[UIFont systemFontOfSize:18]];
    [threeLabel setTextColor:RGBCOLOR(47, 144, 37)];
    
    UILabel *fourLabel = (UILabel*)[_datasource pageAtIndex:pre5 andScrollView:self];
    [fourLabel setFont:[UIFont systemFontOfSize:16]];
    [fourLabel setTextColor:RGBACOLOR(113.0, 113.0, 113.0, 1.0)];
    
    UILabel *preLabel = (UILabel*)[_datasource pageAtIndex:pre andScrollView:self];
    [preLabel setFont:[UIFont systemFontOfSize:14]];
    [preLabel setTextColor:RGBACOLOR(186.0, 186.0, 186.0, 1.0)];
    
    UILabel *lastLabel = (UILabel*)[_datasource pageAtIndex:last andScrollView:self];
    [lastLabel setFont:[UIFont systemFontOfSize:14]];
    [lastLabel setTextColor:RGBACOLOR(186.0, 186.0, 186.0, 1.0)];
    
    [_curViews addObject:oneLabel];
    [_curViews addObject:twoLabel];
    [_curViews addObject:currentLabel];
    [_curViews addObject:threeLabel];
    [_curViews addObject:fourLabel];
    [_curViews addObject:preLabel];
    [_curViews addObject:lastLabel];
    
}

- (NSInteger)validPageValue:(NSInteger)value {
    
    if(value == -1 ) value = _totalPages - 1;
    if(value == _totalPages+1) value = 1;
    if (value == _totalPages+2) value = 2;
    if(value == _totalPages+3) value = 3;
    if (value == _totalPages+4) value = 4;
    if(value == _totalPages) value = 0;
    
    
    return value;
    
}

- (void)handleTap:(UITapGestureRecognizer *)tap {
    
    if ([_delegate respondsToSelector:@selector(didClickPage:atIndex:)]) {
        [_delegate didClickPage:self atIndex:_curPage];
    }
    
}

- (void)setViewContent:(UIView *)view atIndex:(NSInteger)index
{
    if (index == _curPage) {
        [_curViews replaceObjectAtIndex:1 withObject:view];
        for (int i = 0; i < 7; i++) {
            UIView *v = [_curViews objectAtIndex:i];
            v.userInteractionEnabled = YES;
            v.frame = CGRectOffset(v.frame, 0, v.frame.size.height * i);
            [_scrollView addSubview:v];
        }
    }
}

- (void)setAfterScrollShowView:(UIScrollView*)scrollview  andCurrentPage:(NSInteger)pageNumber
{
    UILabel *oneLabel = (UILabel*)[[scrollview subviews] objectAtIndex:pageNumber];
    [oneLabel setFont:[UIFont systemFontOfSize:14]];
    [oneLabel setTextColor:RGBACOLOR(186.0, 186.0, 186.0, 1.0)];
    UILabel *twoLabel = (UILabel*)[[scrollview subviews] objectAtIndex:pageNumber+1];
    [twoLabel setFont:[UIFont systemFontOfSize:16]];
    [twoLabel setTextColor:RGBACOLOR(113.0, 113.0, 113.0, 1.0)];
    
    UILabel *currentLabel = (UILabel*)[[scrollview subviews] objectAtIndex:pageNumber+2];
    [currentLabel setFont:[UIFont systemFontOfSize:18]];
    [currentLabel setTextColor:RGBCOLOR(47, 144, 37)];
    
    UILabel *threeLabel = (UILabel*)[[scrollview subviews] objectAtIndex:pageNumber+3];
    [threeLabel setFont:[UIFont systemFontOfSize:16]];
    [threeLabel setTextColor:RGBACOLOR(113.0, 113.0, 113.0, 1.0)];
    UILabel *fourLabel = (UILabel*)[[scrollview subviews] objectAtIndex:pageNumber+4];
    [fourLabel setFont:[UIFont systemFontOfSize:14]];
    [fourLabel setTextColor:RGBACOLOR(186.0, 186.0, 186.0, 1.0)];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    int y = aScrollView.contentOffset.y;
    NSInteger page = aScrollView.contentOffset.y/((self.bounds.size.height/5));
    if (y>2*(self.bounds.size.height/5)) {
        _curPage = [self validPageValue:_curPage+1];
        [self loadData];
    }
    if (y<=0) {
        _curPage = [self validPageValue:_curPage-1];
        [self loadData];
    }
    if (page>1 || page <=0) {
        [self setAfterScrollShowView:aScrollView andCurrentPage:1];
    }
    if ([_delegate respondsToSelector:@selector(scrollviewDidChangeNumber:)]) {
        [_delegate scrollviewDidChangeNumber:self];
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self setAfterScrollShowView:scrollView andCurrentPage:1];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [_scrollView setContentOffset:CGPointMake(0, (self.bounds.size.height/5)) animated:YES];
    [self setAfterScrollShowView:scrollView andCurrentPage:1];
    if ([_delegate respondsToSelector:@selector(scrollviewDidChangeNumber:)]) {
        [_delegate scrollviewDidChangeNumber:self];
    }
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self setAfterScrollShowView:scrollView andCurrentPage:1];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [_scrollView setContentOffset:CGPointMake(0, (self.bounds.size.height/5)) animated:YES];
    [self setAfterScrollShowView:scrollView andCurrentPage:1];
    if ([_delegate respondsToSelector:@selector(scrollviewDidChangeNumber:)]) {
        [_delegate scrollviewDidChangeNumber:self];
    }
}

@end

合併日期,年月日,時分,星期


//
//  RBCustomDatePickerView.h
//  RBCustomDateTimePicker
//  e-mail:[email protected]
//  Created by renbing on 3/17/14.
//  Copyright (c) 2014 renbing. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "MXSCycleScrollView.h"

@interface RBCustomDatePickerView : UIView <MXSCycleScrollViewDatasource,MXSCycleScrollViewDelegate>

@property (nonatomic, strong) void (^didChooseTime)(id result);

@end

//
//  RBCustomDatePickerView.m
//  RBCustomDateTimePicker
//  e-mail:[email protected]
//  Created by renbing on 3/17/14.
//  Copyright (c) 2014 renbing. All rights reserved.
//

#import "RBCustomDatePickerView.h"
#import "NSCalendar+DateManipulation.h"
#import "NSCalendar+Ranges.h"

@interface RBCustomDatePickerView()
{
    UIView                      *timeBroadcastView;//定時播放顯示視圖
    MXSCycleScrollView          *yearScrollView;//年份滾動視圖
    MXSCycleScrollView          *monthScrollView;//月份滾動視圖
    MXSCycleScrollView          *dayScrollView;//日滾動視圖
    MXSCycleScrollView          *hourScrollView;//時滾動視圖
    MXSCycleScrollView          *minuteScrollView;//分滾動視圖
    NSCalendar *_calendar;
    NSInteger _days;
}
@end

@implementation RBCustomDatePickerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _calendar = [NSCalendar currentCalendar];
        
        [self setTimeBroadcastView];
    }
    return self;
}

#pragma mark -custompicker
//設置自定義datepicker界面
- (void)setTimeBroadcastView
{
    timeBroadcastView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreenWidth, 180.0)];
    CGColorRef cgColor = [UIColor colorWithRed:221.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0].CGColor;
    timeBroadcastView.layer.borderColor = cgColor;
    timeBroadcastView.layer.borderWidth = 1.0;
    [self addSubview:timeBroadcastView];
    
    UIView *middleSepView = [[UIView alloc] initWithFrame:CGRectMake(0, 72.0, kScreenWidth, 36)];
    [middleSepView setBackgroundColor:RGBCOLOR(227, 255, 226)];
    [timeBroadcastView addSubview:middleSepView];
    
    [self setYearScrollView];
    [self setMonthScrollView];
    [self setDayScrollView];
    [self setHourScrollView];
    [self setMinuteScrollView];
}

//設置年月日時分的滾動視圖
- (void)setYearScrollView
{
    yearScrollView = [[MXSCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, 80.0, 180)];
    NSInteger yearint = [self setNowTimeShow:0];
    [yearScrollView setCurrentSelectPage:(yearint-2002)];
    yearScrollView.delegate = self;
    yearScrollView.datasource = self;
    [self setAfterScrollShowView:yearScrollView andCurrentPage:1];
    [timeBroadcastView addSubview:yearScrollView];
}

//設置年月日時分的滾動視圖
- (void)setMonthScrollView
{
    monthScrollView = [[MXSCycleScrollView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(yearScrollView.frame), 0, 50.0, 180)];
    NSInteger monthint = [self setNowTimeShow:1];
    [monthScrollView setCurrentSelectPage:(monthint-3)];
    monthScrollView.delegate = self;
    monthScrollView.datasource = self;
    [self setAfterScrollShowView:monthScrollView andCurrentPage:1];
    [timeBroadcastView addSubview:monthScrollView];
}
//設置年月日時分的滾動視圖
- (void)setDayScrollView
{
    _days = [_calendar daysPerMonthUsingReferenceDate:[NSDate date]];
    
    dayScrollView = [[MXSCycleScrollView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(monthScrollView.frame), 0, 90.0, 180)];
    NSInteger dayint = [self setNowTimeShow:2];
    [dayScrollView setCurrentSelectPage:(dayint-3)];
    dayScrollView.delegate = self;
    dayScrollView.datasource = self;
    [self setAfterScrollShowView:dayScrollView andCurrentPage:1];
    [timeBroadcastView addSubview:dayScrollView];
}
//設置年月日時分的滾動視圖
- (void)setHourScrollView
{
    hourScrollView = [[MXSCycleScrollView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(dayScrollView.frame), 0, 50.0, 180)];
    NSInteger hourint = [self setNowTimeShow:3];
    [hourScrollView setCurrentSelectPage:(hourint-2)];
    hourScrollView.delegate = self;
    hourScrollView.datasource = self;
    [self setAfterScrollShowView:hourScrollView andCurrentPage:1];
    [timeBroadcastView addSubview:hourScrollView];
}
//設置年月日時分的滾動視圖
- (void)setMinuteScrollView
{
    minuteScrollView = [[MXSCycleScrollView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(hourScrollView.frame), 0, 50.0, 180)];
    NSInteger minuteint = [self setNowTimeShow:4];
    [minuteScrollView setCurrentSelectPage:(minuteint-2)];
    minuteScrollView.delegate = self;
    minuteScrollView.datasource = self;
    [self setAfterScrollShowView:minuteScrollView andCurrentPage:1];
    [timeBroadcastView addSubview:minuteScrollView];
}

- (void)setAfterScrollShowView:(MXSCycleScrollView*)scrollview  andCurrentPage:(NSInteger)pageNumber
{
    UILabel *oneLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber];
    [oneLabel setFont:[UIFont systemFontOfSize:14]];
    [oneLabel setTextColor:RGBACOLOR(186.0, 186.0, 186.0, 1.0)];
    UILabel *twoLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber+1];
    [twoLabel setFont:[UIFont systemFontOfSize:16]];
    [twoLabel setTextColor:RGBACOLOR(113.0, 113.0, 113.0, 1.0)];
    
    UILabel *currentLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber+2];
    [currentLabel setFont:[UIFont systemFontOfSize:18]];
    [currentLabel setTextColor:RGBCOLOR(47, 144, 37)];
    
    UILabel *threeLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber+3];
    [threeLabel setFont:[UIFont systemFontOfSize:16]];
    [threeLabel setTextColor:RGBACOLOR(113.0, 113.0, 113.0, 1.0)];
    UILabel *fourLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber+4];
    [fourLabel setFont:[UIFont systemFontOfSize:14]];
    [fourLabel setTextColor:RGBACOLOR(186.0, 186.0, 186.0, 1.0)];
}

#pragma mark mxccyclescrollview delegate
#pragma mark mxccyclescrollview databasesource
- (NSInteger)numberOfPages:(MXSCycleScrollView*)scrollView
{
    if (scrollView == yearScrollView) {
        return 99;
    }
    else if (scrollView == monthScrollView)
    {
        return 12;
    }
    else if (scrollView == dayScrollView)
    {
        return _days;
    }
    else if (scrollView == hourScrollView)
    {
        return 24;
    }
    return 60;
}

- (UIView *)pageAtIndex:(NSInteger)index andScrollView:(MXSCycleScrollView *)scrollView
{
    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, scrollView.bounds.size.width, scrollView.bounds.size.height/5)];
    l.tag = index + 1;
    if (scrollView == yearScrollView) {
        l.text = [NSString stringWithFormat:@"%d年",2000  +index];
    }
    else if (scrollView == monthScrollView)
    {
        l.text = [NSString stringWithFormat:@"%02d月",1+index];
    }
    else if (scrollView == dayScrollView)
    {
        UILabel *yearLabel = [[(UILabel*)[[yearScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:(index - 1) % 7];
        UILabel *monthLabel = [[(UILabel*)[[monthScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:(index - 1) % 7];
        UILabel *dayLabel = l;
        NSInteger yearInt = yearLabel.tag + 1999;
        NSInteger monthInt = monthLabel.tag;
        NSInteger dayInt = dayLabel.tag;
        
        NSString *dateStr = [NSString stringWithFormat:@"%d-%02d-%02d",yearInt, monthInt, dayInt];
        
        l.text = [NSString stringWithFormat:@"%02d日 %@",1 + index, [self getWeekDay:dateStr]];
    }
    else if (scrollView == hourScrollView)
    {
        l.text = [NSString stringWithFormat:@"%02d時",index];
    }
    else if (scrollView == minuteScrollView)
    {
        l.text = [NSString stringWithFormat:@"%02d分",index];
    }
    else
        
        l.text = [NSString stringWithFormat:@"%02d",index];
    
    l.font = [UIFont systemFontOfSize:16];
    l.textColor = RGBACOLOR(113.0, 113.0, 113.0, 1.0);
    l.textAlignment = NSTextAlignmentCenter;
    l.backgroundColor = [UIColor clearColor];
    return l;
}

- (NSString *)getWeekDay:(NSString *)strDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"zh_CN"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *date = [dateFormatter dateFromString:strDate];
    if (date == nil) {
        return @"";
    }
    
    NSDateComponents *dateComponents = [_calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:date];
    // 默認顯示今天
    NSDateComponents *currDateComponents = [_calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
    NSString *weekStr = @"";
    if (dateComponents.year == currDateComponents.year && dateComponents.month == currDateComponents.month && dateComponents.day == currDateComponents.day) {
        weekStr = @"今天";
    } else {
        weekStr = [WEEKARRAY objectAtIndex:dateComponents.weekday - 1];
    }
    
    return weekStr;
}

//設置現在時間
- (NSInteger)setNowTimeShow:(NSInteger)timeType
{
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"zh_CN"]];
    [dateFormatter setDateFormat:@"yyyyMMddHHmm"];
    NSString *dateString = [dateFormatter stringFromDate:now];
    switch (timeType) {
        case 0:
        {
            NSRange range = NSMakeRange(0, 4);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        case 1:
        {
            NSRange range = NSMakeRange(4, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        case 2:
        {
            NSRange range = NSMakeRange(6, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        case 3:
        {
            NSRange range = NSMakeRange(8, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        case 4:
        {
            NSRange range = NSMakeRange(10, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        default:
            break;
    }
    return 0;
}

//滾動時上下標籤顯示(當前時間和是否爲有效時間)
- (void)scrollviewDidChangeNumber:(MXSCycleScrollView *)csView
{
    UILabel *yearLabel = [[(UILabel*)[[yearScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    UILabel *monthLabel = [[(UILabel*)[[monthScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    UILabel *dayLabel = [[(UILabel*)[[dayScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    NSInteger yearInt = yearLabel.tag + 1999;
    NSInteger monthInt = monthLabel.tag;
    NSInteger dayInt = dayLabel.tag;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"zh_CN"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    if (csView == yearScrollView || csView == monthScrollView) {
        NSString *dateStr = [NSString stringWithFormat:@"%d-%02d-01",yearInt, monthInt];
        NSInteger day = [_calendar daysPerMonthUsingReferenceDate:[dateFormatter dateFromString:dateStr]];
        
        if (day != [self numberOfPages:csView]) {
            _days = day;
            
            [dayScrollView reloadData];
        }
    } else if (csView == dayScrollView) {
        NSString *dateStr = [NSString stringWithFormat:@"%d-%02d-%02d",yearInt, monthInt,dayInt];
        
        dayLabel.text = [NSString stringWithFormat:@"%02d日 %@",dayInt, [self getWeekDay:dateStr]];
    }
    
    UILabel *hourLabel = [[(UILabel*)[[hourScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    UILabel *minuteLabel = [[(UILabel*)[[minuteScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    
    NSInteger hourInt = hourLabel.tag - 1;
    NSInteger minuteInt = minuteLabel.tag - 1;
    
    if (self.didChooseTime) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        
        self.didChooseTime([dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%02d-%02d %02d:%02d",yearInt,monthInt,dayInt,hourInt,minuteInt]]);
    }
}

@end

整合顯示信息...

//
//  CustomDatePickerView.h
//  PateoII
//
//  Created by lance on 14/12/4.
//  Copyright (c) 2014年 Pateo. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomDatePickerView : UIView

- (id)initWithParentView:(UIView *)parent;

@property (nonatomic, strong) void (^didChooseTime)(id result);
@property (nonatomic, strong) void (^didCancelTime)();

- (void)show;

@end

//
//  CustomDatePickerView.m
//  PateoII
//
//  Created by lance on 14/12/4.
//  Copyright (c) 2014年 Pateo. All rights reserved.
//

#import "CustomDatePickerView.h"
#import "RBCustomDatePickerView.h"

@interface CustomDatePickerView ()
{
    RBCustomDatePickerView *_pickerView;
    UIView *_translucentView;
    
    UIView *_parent;
}

@end

@implementation CustomDatePickerView

- (id)initWithParentView:(UIView *)parent
{
    CGRect frame = parent.bounds;
    frame.size.height = 180;
    frame.origin.y = CGRectGetMaxY(parent.frame);
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        _translucentView = [[UIView alloc] initWithFrame:parent.bounds];
        _translucentView.backgroundColor = RGBACOLOR(0, 0, 0, 0.5);
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [_translucentView addGestureRecognizer:tapGesture];
        
        
        _pickerView = [[RBCustomDatePickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreenWidth, 180.0)];
        [self addSubview:_pickerView];
        
        __weak CustomDatePickerView *weakSelf = self;
        _pickerView.didChooseTime = ^(id result) {
            if (weakSelf.didChooseTime) {
                weakSelf.didChooseTime(result);
            }
        };
        
        _parent = parent;
    }
    
    return self;
}

/**
 *  關閉 動畫
 */
- (void)hide
{
    CGRect frame = self.frame;
    frame.origin.y = CGRectGetMaxY(_parent.frame);
    [UIView animateWithDuration:0.35 animations:^{
        self.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [_translucentView removeFromSuperview];
    }];
    
    if (self.didCancelTime) {
        self.didCancelTime();
    }
}

/**
 *  顯示 動畫
 */
- (void)show
{
    [_parent addSubview:_translucentView];
    [_parent addSubview:self];
    
    CGRect frame = self.frame;
    frame.origin.y = CGRectGetMaxY(_parent.frame) - CGRectGetHeight(self.frame) - 64.0;
    [UIView animateWithDuration:0.35 animations:^{
        self.frame = frame;
    }];
}

- (void)tapAction
{
    [self hide];
}

@end

控件主要是他人寫的(修改部分算法,比如每月多少天修改爲正常顯示,而不是總是31天),自己學習記錄,使用下...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章