///日历显示部分重要代码
下载源码:http://download.youkuaiyun.com/detail/xcp_123/9722399
//日历显示的页面
#import <UIKit/UIKit.h>
#import "XCPConfig.h"
#import "XCPDateModel.h"
#import "XCPCellDateModel.h"
#import "XCPLinearView.h"
#import "XCPDateTools.h"
#import "XCPCalendarScrollView.h"
#import "XCPCalendarScrollViewCell.h"
@protocol HomeCalendarViewControllerDelegate <NSObject>
@optional
- (void)didCompleteTime:(NSNotification *)completeTime text:(NSString *)text;
@end
@interface HomeCalendarViewController : UIViewController <XCPCalendarScrollViewDelegate>
{
XCPLinearView *_lineView;
NSInteger _currentYear;
NSInteger _currentMonth;
NSInteger _currentDay;
NSInteger _currentWeekday;
NSInteger _lastCellDeviation;
NSMutableDictionary *_cell;
}
@property(nonatomic, strong) id<HomeCalendarViewControllerDelegate> delegate;
@interface HomeCalendarViewController ()
@end
NSString *const CalendarCellIdentifier = @"cell";
@implementation HomeCalendarViewController
- (instancetype)init
{
self = [super init];
if (!self) {
self = [[HomeCalendarViewController alloc] init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSelecteDate:) name:@"DID_SELETED_DATEVIEW" object:nil];
[self.view addSubview:[self getTitleView]];
XCPCalendarScrollView *calenardarScrollView = [[XCPCalendarScrollView alloc] initWithFrame:CGRectMake(0, 50, SWIDTH, SHEIGHT-50)];
calenardarScrollView.backgroundColor = [UIColor clearColor];
calenardarScrollView.decelerationRate = 1.0;
calenardarScrollView.delegateForCell = self;
[self.view addSubview:calenardarScrollView];
}
-(UIView *)getTitleView
{
NSArray *titleArry = @[@"周日",@"一",@"二",@"三",@"四",@"五",@"六"];
CGFloat width = SWIDTH / 7;
UIView *weekView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT-50)];
for (int i = 0; i < titleArry.count; i++) {
UILabel *lab = [[UILabel alloc] init ];
lab.text = titleArry[i];
lab.font = [UIFont systemFontOfSize:18.0];
lab.textAlignment = NSTextAlignmentCenter;
lab.textColor = [UIColor whiteColor];
if (i == 1 || i == 7) {
lab.textColor = [UIColor whiteColor];
}
CGFloat labH = SHEIGHT * 0.215 - 44;
if (SHEIGHT == 736) {
lab.frame = CGRectMake(i * width, 32, width, labH);
}else if ([UIScreen mainScreen].bounds.size.height == 568){
lab.frame = CGRectMake(i * width, 12, width, 18);
}else{
lab.frame = CGRectMake(i * width, 15, width, 20);
}
[weekView addSubview:lab];
}
return weekView;
}
-(XCPLinearView *)getTipLabel:(NSString *)string
{
if (!_lineView) {
_lineView = [[XCPLinearView alloc] initWithFrame:CGRectMake(0, 50, SWIDTH, 50)];
_lineView.backgroundColor = [UIColor clearColor];
_lineView.hidden = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 35)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:14.0];
label.tag = 100;
[_lineView addSubview:label];
_lineView.alpha = 0.0;
[self.view addSubview:_lineView];
}
UILabel *label = [_lineView viewWithTag:100];
label.text = string;
return _lineView;
}
-(void)didSelecteDate:(NSNotification *)notification
{
NSDictionary * dict = notification.userInfo;
XCPDateModel *model = (XCPDateModel *)[dict objectForKey:@"dateModel"];
int week = (int)model.weekday;
NSString *str_week;
switch (week) {
case 1:
str_week = @"周一";
break;
case 2:
str_week = @"周二";
break;
case 3:
str_week = @"周三";
break;
case 4:
str_week = @"周四";
break;
case 5:
str_week = @"周五";
break;
case 6:
str_week = @"周六";
break;
case 7:
str_week = @"周日";
break;
default:
break;
}
NSString *toString = [NSString stringWithFormat:@"%ld-%ld-%ld %@",(long)model.year,(long)model.month,(long)model.day,str_week];
if (_delegate && [_delegate respondsToSelector:@selector(didCompleteTime:text:)]) {
[_delegate didCompleteTime:notification text:toString];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
-(XCPCalendarScrollViewCell *)calendarScrollViewCellWithDeviation:(NSInteger)deviation calendarScrollView:(XCPCalendarScrollView *)calendarScrollView
{
static NSString *cellID = @"cellID";
XCPCalendarScrollViewCell *cell = [calendarScrollView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[XCPCalendarScrollViewCell alloc] initWithIdentifier:cellID];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
XCPCellDateModel *cellDateModel = [XCPDateTools dateToCell:deviation];
dispatch_async(dispatch_get_main_queue(), ^{
[cell fillDate:cellDateModel];
});
});
return cell;
}
-(CGFloat)calendarScrollViewCellHeightWithdeviation:(NSInteger)deviation calendarScrollView:(XCPCalendarScrollView *)calendarScrollView{
NSInteger row = [XCPDateTools getDrawRow:deviation];
CGFloat width = (self.view.frame.size.width-20)/7.0;
return (width+15)*row+10+30;
}
-(void)didSelectedWithDeviation:(NSInteger)deviation calendarScrollView:(XCPCalendarScrollView *)calendarScrollView
{
// NSLog(@"%ld",(long)deviation);
}
-(void)calendarScrollViewArriveTopVisible:(NSInteger)deviation
{
NSDateComponents *components = [XCPDateTools getCellMonthDate:deviation];
NSString *string = [NSString stringWithFormat:@"%ld年%ld月",(long)[components year],(long)[components month]];
XCPLinearView *lineView = [self getTipLabel:string];
if (lineView.hidden == YES) {
lineView.hidden = NO;
[UIView animateWithDuration:0.3 animations:^{
lineView.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.6 animations:^{
lineView.alpha = 0.6;
} completion:^(BOOL finished) {
lineView.hidden = YES;;
}];
}];
}
}
#import <Foundation/Foundation.h>
#import "XCPCellDateModel.h"
#import "XCPDateModel.h"
@class XCPCellDateModel;
@interface XCPDateTools : NSObject
+(XCPCellDateModel *)dateToCell:(NSInteger)deviation;
+(NSInteger)getDrawRow:(NSInteger)deviation;
+(NSDateComponents *)getCurrentDate:(NSInteger)deviation;
+(NSDateComponents *)getCellMonthDate:(NSInteger)deviation;
#import "XCPDateTools.h"
@implementation XCPDateTools
+(XCPCellDateModel *)dateToCell:(NSInteger)deviation
{
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:30];
NSDateComponents *components = [XCPDateTools getCellMonthDate:deviation];
NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];
NSInteger weekday = [components weekday];
NSInteger monthDays = [XCPDateTools getMonthDays:month year:year];
XCPCellDateModel *cellDateModel = [[XCPCellDateModel alloc] init];
cellDateModel.year = year;
cellDateModel.month = month;
cellDateModel.monthDays = monthDays;
cellDateModel.beginWeekDay = weekday;
NSInteger row = 0;
NSInteger dayBeginIndex = weekday-1;
if((monthDays + dayBeginIndex)%7 == 0){
row = (monthDays + dayBeginIndex)/7;
}else{
row = (monthDays + dayBeginIndex)/7 + 1;
}
cellDateModel.drawDayRow = row;
cellDateModel.drawDayBeginIndex = dayBeginIndex;
for (int i = 0; i < monthDays; i++) {
XCPDateModel *dateModel = [[XCPDateModel alloc] init];
dateModel.year = year;
dateModel.month = month;
dateModel.day = day;
dateModel.weekday = (dayBeginIndex+day-1)%7;
dateModel.lunarDay = [self getChineseCalendarWithDate:day month:month year:year];
day++;
[array addObject:dateModel];
}
cellDateModel.dateModelArray = array;
return cellDateModel;
}
+(NSInteger)getDrawRow:(NSInteger)deviation
{
NSDateComponents *components = [XCPDateTools getCellMonthDate:deviation];
NSInteger year = [components year];
NSInteger month = [components month];
NSInteger weekday = [components weekday];
NSInteger monthDays = [XCPDateTools getMonthDays:month year:year];
NSInteger row = 0;
NSInteger dayBeginIndex = weekday-1;
if((monthDays + dayBeginIndex)%7 == 0){
row = (monthDays + dayBeginIndex)/7;
}else{
row = (monthDays + dayBeginIndex)/7 + 1;
}
return row;
}
+(NSDateComponents *)getCellMonthDate:(NSInteger)deviation
{
NSDateComponents *comps = [XCPDateTools getCurrentDate:deviation];
// NSLog(@"%@",comps);
NSInteger month = [comps month];
NSInteger year = [comps year];
NSInteger yearDeviation;
NSInteger monthDeviation;
if (deviation > 0) {
yearDeviation = deviation/12;
monthDeviation = deviation % 12;
if (monthDeviation+month > 12 ) {
month = monthDeviation + month - 12;
yearDeviation++;
} else {
month = month + monthDeviation;
}
} else {
yearDeviation = deviation / 12;
monthDeviation = deviation % 12;
if (monthDeviation + month <= 0) {
month = month + monthDeviation + 12;
yearDeviation--;
} else {
month = month + monthDeviation;
}
}
year = year + yearDeviation;
NSString* string;
if(month < 10)
{
string = [NSString stringWithFormat:@"%ld0%ld01",(long)year,(long)month];
} else {
string = [NSString stringWithFormat:@"%ld%ld01",(long)year,(long)month];
}
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
inputFormatter.timeZone = [NSTimeZone systemTimeZone];
[inputFormatter setDateFormat:@"yyyyMMdd"];
NSDate* inputDate = [inputFormatter dateFromString:string];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday;
components = [calendar components:unitFlags fromDate:inputDate];
return components;
}
//日历的显示
+(NSDateComponents *)getCurrentDate:(NSInteger)deviation
{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *now;
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday;
now = [[NSDate alloc] init];
comps = [calendar components:unitFlags fromDate:now];
return comps;
}
+(NSInteger)getMonthDays:(NSInteger)month year:(NSInteger)year
{
if (month<=0 || month > 12) {
return 0;
}
BOOL isLeapYear = [XCPDateTools isLeapYear:year];
int februaryDay;
if (isLeapYear) {
februaryDay = 29;
}
else
{
februaryDay = 28;
}
if (month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12) {
return 31;
} else if (month == 4||month ==6||month ==9||month ==11) {
return 30;
}else {
return februaryDay;
}
}
+(BOOL)isLeapYear:(NSInteger)year{
if ((year % 4 == 0 && year % 100 != 0)|| year % 400 == 0)
return YES;
else
return NO;
}
+(NSString*)getChineseCalendarWithDate:(NSInteger)day month:(NSInteger)month year:(NSInteger)year{
NSArray *chineseDays=[NSArray arrayWithObjects:
@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
@"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil];
NSString* string;
if(month<10)
{
if (day < 10) {
string = [NSString stringWithFormat:@"%ld0%ld0%ld23",(long)year,(long)month,(long)day];
}
else{
string = [NSString stringWithFormat:@"%ld0%ld%ld23",(long)year,(long)month,(long)day];
}
}
else
{
if (day < 10) {
string = [NSString stringWithFormat:@"%ld%ld0%ld23",(long)year,(long)month,(long)day];
}
else{
string = [NSString stringWithFormat:@"%ld%ld%ld23",(long)year,(long)month,(long)day];
}
}
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyyMMddHH"];
NSDate *inputDate = [inputFormatter dateFromString:string];
NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:inputDate];
NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];
return d_str;
}
#import <Foundation/Foundation.h>
@interface XCPDateModel : NSObject
@property(nonatomic,assign)NSInteger day;
@property(nonatomic,assign)NSInteger month;
@property(nonatomic,assign)NSInteger year;
@property(nonatomic,assign)NSInteger weekday;
@property(nonatomic,assign)NSString *lunarDay;
@end
#import <Foundation/Foundation.h>
@class XCPDateModel;
@interface XCPCellDateModel : NSObject
@property(nonatomic,strong)NSArray <__kindof XCPDateModel *> *dateModelArray;
@property(nonatomic,assign)NSInteger drawDayBeginIndex;
@property(nonatomic,assign)NSInteger drawDayRow;
@property(nonatomic,assign)NSInteger year;
@property(nonatomic,assign)NSInteger month;
@property(nonatomic,assign)NSInteger monthDays;
@property(nonatomic,assign)NSInteger beginWeekDay;
@end
//日历的显示模式 判断时间是当前时间则显示红色圆背景
//上下滑动即显示上下月份
注:原文版权归作者所有,转载请注明出处