Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions 21651012尹益鹏/project02/Calendar/Calendar/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AppDelegate.h
// Calendar
//
// Created by yinyipeng on 16/11/10.
// Copyright © 2016年 snailset. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

58 changes: 58 additions & 0 deletions 21651012尹益鹏/project02/Calendar/Calendar/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// AppDelegate.m
// Calendar
//
// Created by yinyipeng on 16/11/10.
// Copyright © 2016年 snailset. All rights reserved.
//

#import "AppDelegate.h"
#import "CalendarViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
CalendarViewController *rootViewController = [[CalendarViewController alloc] init];
rootViewController.view.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = rootViewController;

[self.window makeKeyAndVisible];
return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
19 changes: 19 additions & 0 deletions 21651012尹益鹏/project02/Calendar/Calendar/CalendarModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// CalendarModel.h
// Calendar
//
// Created by yinyipeng on 16/11/10.
// Copyright © 2016年 snailset. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CalendarModel : NSObject

@property (nonatomic)NSInteger month;
@property (nonatomic)NSInteger year;

- (void)setYear:(NSInteger)year andMonth:(NSInteger)month;
- (NSArray *)getMonthModel;

@end
89 changes: 89 additions & 0 deletions 21651012尹益鹏/project02/Calendar/Calendar/CalendarModel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// CalendarModel.m
// Calendar
//
// Created by yinyipeng on 16/11/10.
// Copyright © 2016年 snailset. All rights reserved.
//

#import "CalendarModel.h"

@interface CalendarModel ()

@end

@implementation CalendarModel
{
NSArray *monthModel;
}

- (instancetype)init
{
self = [super init];
if (self) {
[self setYear:[self currentYear] andMonth:[self currentMonth]];
}
return self;
}

- (void)setYear:(NSInteger)year andMonth:(NSInteger)month
{
self.month = month;
self.year = year;
[self calculateMonthModel];
}
//获取月份模型,以6 * 7数组存储
- (void)calculateMonthModel
{
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1];
[components setMonth:self.month];
[components setYear:self.year];
NSDate *date = [calendar dateFromComponents:components];
components = [calendar components:NSCalendarUnitWeekday fromDate:date];
NSInteger weekday = components.weekday;
NSInteger daysNumber = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:42];
for (NSInteger i = 0; i < weekday - 1; i++)
{
[mutableArray addObject:@0];
}
for (NSInteger i = 1; i < daysNumber + 1; i++)
{
[mutableArray addObject:[NSNumber numberWithInteger:i]];
}
for (NSInteger i = 0; i < 42 - daysNumber - weekday + 1; i++)
{
[mutableArray addObject:@0];
}
monthModel = [NSArray arrayWithArray:mutableArray];
}

-(NSArray *)getMonthModel
{
return monthModel;
}

- (NSInteger)currentYear
{
NSDate *date =[NSDate date];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
components = [calendar components:NSCalendarUnitYear fromDate:date];
//NSLog(@"components.year: %ld", components.year);
return components.year;
}

- (NSInteger)currentMonth
{
NSDate *date =[NSDate date];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
components = [calendar components:NSCalendarUnitMonth fromDate:date];
//NSLog(@"components.month: %ld", components.month);
return components.month;
}


@end
18 changes: 18 additions & 0 deletions 21651012尹益鹏/project02/Calendar/Calendar/CalendarView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// CalendarView.h
// Calendar
//
// Created by yinyipeng on 16/11/10.
// Copyright © 2016年 snailset. All rights reserved.
//

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

@interface CalendarView : UIView

@property (nonatomic)CalendarModel *calendarModel;

- (CGSize)minBoundingSize;

@end
107 changes: 107 additions & 0 deletions 21651012尹益鹏/project02/Calendar/Calendar/CalendarView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// CalendarView.m
// Calendar
//
// Created by yinyipeng on 16/11/10.
// Copyright © 2016年 snailset. All rights reserved.
//
//printf(" %s %ld\n", [getMonthString(month) UTF8String],(long)year);
//printf("日 一 二 三 四 五 六\n");
//for (int i = 0; i < 6; i++)
//{
// for (int j = 0; j < 7; j++)
// {
// if (model[i][j] == 0) printf(" ");
// else printf("%2ld ", model[i][j]);
// }
// printf("\n");
//}

#import "CalendarView.h"

@interface CalendarView ()

@property (nonatomic)NSArray *monthStringArray;
@property (nonatomic)NSArray *monthModel;
//@property (nonatomic)NSString *stringToDraw;

@end

@implementation CalendarView

- (instancetype)init
{
return [self initWithCalendarModel:[[CalendarModel alloc] init]];
}

- (instancetype)initWithCalendarModel: (CalendarModel *)calendarModel
{
self = [super init];
if (self != nil)
{
self.calendarModel = calendarModel;
}

return self;
}

- (void)drawRect:(CGRect)rect
{
//[super drawRect:rect];
// CGSize size = [_stringToDraw sizeWithAttributes:nil];
// CGRect newRect = rect;
// newRect.size = size;
[super drawRect:rect];
NSString *string = [self transformCalendarModelIntoString];

[string drawAtPoint:CGPointMake(0, 0) withAttributes:nil];
}

- (NSString *)transformCalendarModelIntoString
{
//NSLog(@"[self.calendarModel.month - 1]: %ld", self.calendarModel.month - 1);
NSMutableString *string = [[NSMutableString alloc] init];
//NSMutableString *string = [NSMutableString stringWithFormat:@" %@ %ld\n", self.monthStringArray[self.calendarModel.month - 1], (long)self.calendarModel.year];
[string appendString:@"Sun Mon Tue Wed Thu Fri Sat\n\n"];
//[string appendString:@"日 一 二 三 四 五 六\n"];
for (int i = 0; i < 42; i++)
{
NSInteger value = [((NSNumber *)self.monthModel[i]) integerValue];
if (value == 0) [string appendString:@" "];
else if (value < 10) [string appendFormat:@" %ld ", value];
else [string appendFormat:@" %ld ", value];
//NSLog(@"\n%2d ", 6);
if (i % 7 == 6) [string appendString:@"\n\n"];
}
//NSLog(@"%@", string);
return [NSString stringWithString:string];

}

- (CGSize)minBoundingSize
{
NSString *string = [self transformCalendarModelIntoString];
return [string sizeWithAttributes:nil];
}

#pragma mark - Accessor Methods

- (NSArray *)monthStringArray
{
if (_monthStringArray == nil)
{
_monthStringArray = @[@"Jan", @"Feb", @"Mar", @"Apr", @"May", @"Jun", @"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec"];
//_monthStringArray = @[@"一月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月", @"十月", @"十一月", @"十二月"];
}
return _monthStringArray;
}

- (void)setCalendarModel:(CalendarModel *)calendarModel
{
_calendarModel = calendarModel;
self.monthModel = [calendarModel getMonthModel];
}



@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// CalendarViewController.h
// Calendar
//
// Created by yinyipeng on 16/11/10.
// Copyright © 2016年 snailset. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CalendarViewController : UIViewController

@end
Loading