iOS自定义日期picker选择器,参考他人代码...
2014-12-05 15:47
435 查看
类似于ios系统自带的日期选择器...
循环滑动类...
//
// MXSCycleScrollView.h
// xuexin
// e-mail:rbyyy924805@163.com
// 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
合并日期,年月日,时分,星期
//
// RBCustomDatePickerView.h
// RBCustomDateTimePicker
// e-mail:rbyyy924805@163.com
// 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
整合显示信息...
//
// 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
控件主要是他人写的(修改部分算法,比如每月多少天修改为正常显示,而不是总是31天),自己学习记录,使用下...
循环滑动类...
//
// MXSCycleScrollView.h
// xuexin
// e-mail:rbyyy924805@163.com
// 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:rbyyy924805@163.com // 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:rbyyy924805@163.com
// 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:rbyyy924805@163.com // 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天),自己学习记录,使用下...
相关文章推荐
- ios DatePicker 自定义,参考他人代码,整理...优化, 循环滑动scrollview
- iOS自定义日期、时间、城市选择器实例代码
- Android自定义DataTimePicker实例代码(日期选择器)
- ios自定义选择器ActionSheetPicker改进版
- Android自定义DataTimePicker(日期选择器)
- 自定义的iOS的滚轮选择器,附带农历选择器实现代码
- iOS自定义日期、时间、城市选择器
- 自定义iOS选择器,附农历选择器代码
- ios 很好用的时间选择器DatePickerView,3行代码搞定
- Android自定义DataTimePicker(日期选择器)
- iOS实现自定义日期选择器示例
- Android自定义DataTimePicker(日期选择器)
- Android自定义DataTimePicker(日期选择器)
- Android自定义DataTimePicker(日期选择器)
- Android自定义DataTimePicker(日期选择器)
- ios开发 自定义日期选择器 PTXDatePickerView
- Android开发之自定义DataTimePicker(日期时间选择器)
- Android自定义DataTimePicker(日期选择器)
- Android自定义DataTimePicker(日期选择器)
- Android自定义DataTimePicker(日期选择器)