///日歷顯示局部重要代碼
下載源碼:http://download.csdn.net/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 ()
}
#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
//日歷的顯示形式 判別時間是以後時間則顯示白色圓背景
注:原文版權歸作者一切,轉載請注明出處
原文:http://download.csdn.net/detail/xcp_123/9722399
【iOS 日歷顯示及規則某件事的完成時間(可選)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!