#import <UIKit/UIKit.h>
#define But_TextFontColor [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]
#define But_TextFontColor_Highlight [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]
@interface ViewController : UIViewController{
IBOutlet UIView *headerView;
//当前选中年份
int selectedYear;
}
@property (nonatomic,assign)int selectedYear;
-(BOOL)isLeapYear:(int)year;
-(int)callWeek:(int)y:(int)m:(int)d;
-(void)reloadCalendar:(int)year;
@end
#import "ViewController.h"
#import "CellViewController.h"
@implementation ViewController
@synthesize selectedYear;
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
for (int v = 1; v <= 12; v++) {
//月份视图
UIView *view = [[UIView alloc]init];
[view setBackgroundColor:[UIColor clearColor]];
[view setTag:(1000+v)];
//头部背景
UIView *viewTitle = [[UIView alloc]init];
[viewTitle setFrame:CGRectMake(0, 0, 218, 50)];
[viewTitle setBackgroundColor:[UIColor lightGrayColor]];
[view addSubview:viewTitle];
//月份lab
UILabel *labMounth = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 50, 20)];
[labMounth setBackgroundColor:[UIColor clearColor]];
[labMounth setTextColor:[UIColor whiteColor]];
[labMounth setFont:[UIFont boldSystemFontOfSize:17]];
[labMounth setText:[NSString stringWithFormat:@"%d月",v]];
[view addSubview:labMounth];
//星期lab
for (int l = 1; l<=7 ; l++) {
int B_X = (l-1)%7*30 + ((l-1)%7+1);
UILabel *labWeek = [[UILabel alloc]initWithFrame:CGRectMake(B_X, 25, 30, 25)];
[labWeek setBackgroundColor:[UIColor clearColor]];
[labWeek setTextColor:[UIColor blackColor]];
[labWeek setFont:[UIFont boldSystemFontOfSize:14]];
[labWeek setTextAlignment:UITextAlignmentCenter];
if (l==1) {
[labWeek setText:@"周一"];
} else if (l==2) {
[labWeek setText:@"周二"];
} else if (l==3) {
[labWeek setText:@"周三"];
} else if (l==4) {
[labWeek setText:@"周四"];
} else if (l==5) {
[labWeek setText:@"周五"];
} else if (l==6) {
[labWeek setText:@"周六"];
} else if (l==7) {
[labWeek setText:@"周日"];
}
[labWeek setTag:(2000+l)];
[view addSubview:labWeek];
}
//天button
for (int b = 1; b <= 42; b++) {
int B_X = (b-1)%7*30 + ((b-1)%7+1);
int B_Y = 50 + (b-1)/7*25 + ((b-1)/7+1);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:(3000+b)];
[button setBackgroundImage:[UIImage imageNamed:@"WDL_white.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"WDL_black.png"] forState:UIControlStateHighlighted];
[button setFrame:CGRectMake(B_X, B_Y, 30, 25)];
[button setTitleColor:But_TextFontColor forState:UIControlStateHighlighted];
[button setTitleColor:But_TextFontColor_Highlight forState:UIControlStateNormal];
[view addSubview:button];
}
[self.view addSubview:view];
}
[self reloadCalendar:0];
}
-(void)reloadCalendar:(int)year{
if (year == 0) {
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [calendar components:unitFlags fromDate:date];
self.selectedYear = [components year];
} else {
self.selectedYear = year;
}
for (int v = 1; v <= 12; v++) {
UIView *view = [self.view viewWithTag:(1000+v)];
//当前月有多少天
int days;
if (v == 1 || v == 3 || v == 5 || v == 7 || v == 8 || v == 10 || v == 12) {
days = 31;
} else if (v == 2){
if ([self isLeapYear:self.selectedYear]) {
days = 29;
} else {
days = 28;
}
} else {
days = 30;
}
//当前月的第一天是周几
int firstWeek = [self callWeek:self.selectedYear :v :1];
int day = 1;
for (int b = 1; b <= 42; b++) {
UIButton *button = (UIButton*)[view viewWithTag:(3000+b)];
if (b < firstWeek) {
[button setHidden:YES];
} else if (b > ((firstWeek - 1) + days)) {
[button setHidden:YES];
} else {
[button setTitle:[NSString stringWithFormat:@"%d",day] forState:UIControlStateNormal];
day ++;
}
}
}
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
for (int i = 0; i < 12; i++) {
int V_X = i%4*218 + (i%4+1)*30;
int V_Y = 36 + i/4*207 + (i/4+1)*22;
UIView *cellView = [self.view viewWithTag:(1000+i+1)];
[cellView setFrame:CGRectMake(V_X, V_Y, 218, 207)];
}
[headerView setFrame:CGRectMake(0, 0, 1024, 36)];
} else {
for (int i = 0; i < 12; i++) {
int V_X = i%3*218 + (i%3+1)*28;
int V_Y = 36 + i/3*207 + (i/3+1)*28;
UIView *cellView = [self.view viewWithTag:(1000+i+1)];
[cellView setFrame:CGRectMake(V_X, V_Y, 218, 207)];
}
[headerView setFrame:CGRectMake(0, 0, 768, 36)];
}
return YES;
}
#pragma mark - 判断闰年
/*
基于一年有365.242199174 日,而并非刚刚好的365 日,故每四年有一次闰年,即二月多了第二十九日。
经过四年一闰后,已修正为365.25,但仍有误差,故每一百年就会减一个闰年,即1700,1800,1900年等均没有闰年,再修正后为365.24。
最后每四百年加回一个闰年,即1600年、2000 年、2400年等均有闰年,最后修正为365.2425。仍有0.0003 误差,需要约3000年才会出现一天误差,所以已经很准确了。
*/
-(BOOL)isLeapYear:(int)year{
if (year%400 == 0) {
return YES;
} else {
if (year%4==0 && year%100!=0) {
return YES;
} else {
return NO;
}
}
}
-(int)callWeek:(int)y:(int)m:(int)d{
if (m == 1) {
m = 13;
y--;
} else if (m == 2) {
m = 14;
y--;
}
int c = y / 100;
y = y - c * 100;
int week = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
week %= 7;
week = week < 0 ? week + 7 : week;
if (week == 0) {
week = 7;
}
return week;
}
@end