From 24e31e4e317514d8d395dc0e55eab560485408e3 Mon Sep 17 00:00:00 2001 From: snailset Date: Mon, 14 Nov 2016 15:10:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E5=92=8C=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Calendar/Calendar/AppDelegate.h" | 17 + .../Calendar/Calendar/AppDelegate.m" | 58 ++++ .../Base.lproj/LaunchScreen.storyboard" | 27 ++ .../Calendar/Calendar/CalendarModel.h" | 19 ++ .../Calendar/Calendar/CalendarModel.m" | 89 ++++++ .../Calendar/Calendar/CalendarView.h" | 18 ++ .../Calendar/Calendar/CalendarView.m" | 107 +++++++ .../Calendar/CalendarViewController.h" | 13 + .../Calendar/CalendarViewController.m" | 271 ++++++++++++++++ .../project02/Calendar/Calendar/Info.plist" | 34 ++ .../project02/Calendar/Calendar/Macro.h" | 30 ++ .../Calendar/MonthAndYearPickerView.h" | 27 ++ .../Calendar/MonthAndYearPickerView.m" | 122 +++++++ .../Calendar/UIView+FrameProcessor.h" | 20 ++ .../Calendar/UIView+FrameProcessor.m" | 85 +++++ .../Calendar/YearAndMonthButtonView.h" | 19 ++ .../Calendar/YearAndMonthButtonView.m" | 161 ++++++++++ .../project02/Calendar/Calendar/main.m" | 16 + .../TaskList/TaskList/AppDelegate.h" | 17 + .../TaskList/TaskList/AppDelegate.m" | 68 ++++ .../AppIcon.appiconset/Contents.json" | 48 +++ .../Base.lproj/LaunchScreen.storyboard" | 27 ++ .../project03/TaskList/TaskList/Info.plist" | 36 +++ .../TaskList/TaskList/TaskListModel.h" | 26 ++ .../TaskList/TaskList/TaskListModel.m" | 85 +++++ .../TaskList/TaskListTableViewController.h" | 16 + .../TaskList/TaskListTableViewController.m" | 300 ++++++++++++++++++ .../project03/TaskList/TaskList/main.m" | 16 + 28 files changed, 1772 insertions(+) create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/AppDelegate.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/AppDelegate.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Base.lproj/LaunchScreen.storyboard" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarModel.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarModel.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarView.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarView.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarViewController.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarViewController.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Info.plist" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Macro.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/MonthAndYearPickerView.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/MonthAndYearPickerView.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/UIView+FrameProcessor.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/UIView+FrameProcessor.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/YearAndMonthButtonView.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/YearAndMonthButtonView.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/main.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/AppDelegate.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/AppDelegate.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Assets.xcassets/AppIcon.appiconset/Contents.json" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Base.lproj/LaunchScreen.storyboard" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Info.plist" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListModel.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListModel.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListTableViewController.h" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListTableViewController.m" create mode 100644 "21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/main.m" diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/AppDelegate.h" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/AppDelegate.h" new file mode 100644 index 0000000..c158f80 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/AppDelegate.h" @@ -0,0 +1,17 @@ +// +// AppDelegate.h +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import + +@interface AppDelegate : UIResponder + +@property (strong, nonatomic) UIWindow *window; + + +@end + diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/AppDelegate.m" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/AppDelegate.m" new file mode 100644 index 0000000..38486ea --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/AppDelegate.m" @@ -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 diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Base.lproj/LaunchScreen.storyboard" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Base.lproj/LaunchScreen.storyboard" new file mode 100644 index 0000000..fdf3f97 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Base.lproj/LaunchScreen.storyboard" @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarModel.h" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarModel.h" new file mode 100644 index 0000000..0d2587f --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarModel.h" @@ -0,0 +1,19 @@ +// +// CalendarModel.h +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import + +@interface CalendarModel : NSObject + +@property (nonatomic)NSInteger month; +@property (nonatomic)NSInteger year; + +- (void)setYear:(NSInteger)year andMonth:(NSInteger)month; +- (NSArray *)getMonthModel; + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarModel.m" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarModel.m" new file mode 100644 index 0000000..e552d65 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarModel.m" @@ -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 diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarView.h" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarView.h" new file mode 100644 index 0000000..da81877 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarView.h" @@ -0,0 +1,18 @@ +// +// CalendarView.h +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import +#import "CalendarModel.h" + +@interface CalendarView : UIView + +@property (nonatomic)CalendarModel *calendarModel; + +- (CGSize)minBoundingSize; + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarView.m" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarView.m" new file mode 100644 index 0000000..8e4963c --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarView.m" @@ -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 diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarViewController.h" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarViewController.h" new file mode 100644 index 0000000..2077a79 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarViewController.h" @@ -0,0 +1,13 @@ +// +// CalendarViewController.h +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import + +@interface CalendarViewController : UIViewController + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarViewController.m" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarViewController.m" new file mode 100644 index 0000000..2de176f --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/CalendarViewController.m" @@ -0,0 +1,271 @@ +// +// CalendarViewController.m +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import "CalendarViewController.h" +#import "CalendarView.h" +#import "CalendarModel.h" +#import "YearAndMonthButtonView.h" +#import "MonthAndYearPickerView.h" + +#import "UIView+FrameProcessor.h" +#import "Macro.h" + + +@interface CalendarViewController () + +@property (nonatomic) CalendarView *calendarView;//显示当月天数分布 +@property (nonatomic) NSArray *monthStringArray;//各月份对应的string +@property (nonatomic) MonthAndYearPickerView *monthAndYearPickerView;//选择年月的picker view +@property (nonatomic) YearAndMonthButtonView *yearAndMonthButtonView;//显示当前年月的button,点击可调出或隐藏picker view +@property (nonatomic) UIView *calendarContainerView;//包在calendarView外面的view + + + +@end + +@implementation CalendarViewController +{ + BOOL isShowingPickerView;//标识选择年份和月份的picker view是否在显示中 +} + +- (void)viewDidLoad { + [super viewDidLoad]; + [self configureViews]; + isShowingPickerView = NO; + // Do any additional setup after loading the view. +} + +- (void)configureViews +{ + [self.view addSubview:self.calendarContainerView]; + self.yearAndMonthButtonView.origin = CGPointMake(self.view.bounds.size.width/ 2, kScreenHeight * 100.0 / 568); + [self.view addSubview:self.yearAndMonthButtonView];//显示当前年月的button,点击可调出或隐藏picker view +} + +#pragma mark - Accessor Methods +- (CalendarView *)calendarView +{ + if (_calendarView == nil) + { + _calendarView = [[CalendarView alloc] init]; + _calendarView.backgroundColor = [UIColor whiteColor]; + CGSize size = [_calendarView minBoundingSize]; + CGFloat width = ceil(size.width); + CGFloat height = ceil(size.height); + _calendarView.size = CGSizeMake(width, height); + } + + return _calendarView; +} + +- (UIView *)calendarContainerView +{ + if (_calendarContainerView == nil) + { + _calendarContainerView = [[UIView alloc] init]; + _calendarContainerView.backgroundColor = [UIColor whiteColor]; + _calendarContainerView.size = CGSizeMake(self.calendarView.size.width * 1.2, self.calendarView.size.height * 1.2); + _calendarContainerView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2); + self.calendarView.center = [_calendarContainerView convertPoint:_calendarContainerView.center fromView:self.view]; + [_calendarContainerView addSubview:self.calendarView]; + + _calendarContainerView.layer.shadowOffset = CGSizeMake(-5.0, 0.0); + _calendarContainerView.layer.shadowRadius = 5.0; + _calendarContainerView.layer.shadowOpacity = 0.4; + } + + return _calendarContainerView; +} + +- (YearAndMonthButtonView *)yearAndMonthButtonView +{ + if (_yearAndMonthButtonView == nil) + { + _yearAndMonthButtonView = [[YearAndMonthButtonView alloc] init]; + [_yearAndMonthButtonView addTarget:self action:@selector(yearAndMonthButtonClicked) forControlEvents:UIControlEventTouchUpInside]; + [_yearAndMonthButtonView sizeToFit]; + } + + return _yearAndMonthButtonView; +} + +- (MonthAndYearPickerView *)monthAndYearPickerView +{ + if (_monthAndYearPickerView == nil) + { + _monthAndYearPickerView = [[MonthAndYearPickerView alloc] init]; + //_monthAndYearPickerView.size = CGSizeMake(kScreenWidth, kScreenHeight / 2); + _monthAndYearPickerView.size = self.calendarContainerView.size; + _monthAndYearPickerView.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2); + _monthAndYearPickerView.actionDelegate = self; + _monthAndYearPickerView.pickerViewDelegate = self; + _monthAndYearPickerView.pickerViewDataSource = self; + _monthAndYearPickerView.transform = CGAffineTransformMakeScale(0.1, 0.1); + _monthAndYearPickerView.hidden = YES; + //_monthAndYearPickerView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1]; + //_monthAndYearPickerView.layer.zPosition = 10; +// _monthAndYearPickerView.layer.shadowOffset = CGSizeMake(-5.0, 0.0); +// _monthAndYearPickerView.layer.shadowRadius = 5.0; +// _monthAndYearPickerView.layer.shadowOpacity = 0.4; + + [self.view addSubview:_monthAndYearPickerView]; + } + + return _monthAndYearPickerView; +} + + +- (NSArray *)monthStringArray +{ + if (_monthStringArray == nil) + { + _monthStringArray = @[@"Jan", @"Feb", @"Mar", @"Apr", @"May", @"Jun", @"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec"]; + //_monthStringArray = @[@"一月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月", @"十月", @"十一月", @"十二月"]; + } + return _monthStringArray; +} + +#pragma mark - UIPickerViewDataSource +- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView +{ + return 2; +} + +- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component +{ + if (component == 0) return 20;//前十年和后十年可以选择 + return 12;//12个月份 +} + +#pragma mark - UIPickerViewDelegate +-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component +{ + return pickerView.size.width / 2; +} + +- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component +{ + return pickerView.size.height / 6; + //return kScreenHeight * 44.0 / 568; +} + +- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component +{ + if (component == 0) return [NSString stringWithFormat:@"%ld", [self currentYear] + row - 10]; + return self.monthStringArray[row]; +} +#pragma mark - ActionDelegate +//点击确定 +- (void)didClickOKButton +{ + NSLog(@"didClickOKButton"); + NSInteger year = [self currentYear] + [self.monthAndYearPickerView selectedRowInComponent:0] - 10; + NSInteger month = 1 + [self.monthAndYearPickerView selectedRowInComponent:1]; + self.yearAndMonthButtonView.year = year; + self.yearAndMonthButtonView.month = month; + CalendarModel *newModel = [[CalendarModel alloc] init]; + [newModel setYear:year andMonth:month]; + self.calendarView.calendarModel = newModel; + [self.calendarView setNeedsDisplay]; + [self hideMonthAndYearPickerView]; +} +//点击取消 +- (void)didClickCancelButton +{ + NSLog(@"didClickCancelButton"); + [self hideMonthAndYearPickerView]; +} + + +//显示picker view +- (void)showMonthAndYearPickerView +{ + NSLog(@"showMonthAndYearPickerView"); + self.monthAndYearPickerView.hidden = NO; + //self.obscureButtonView.hidden = NO; + isShowingPickerView = YES; + [self.yearAndMonthButtonView toogleArrowImage]; + [UIView animateWithDuration:0.3 animations:^{ + //self.calendarContainerView.center = CGPointMake(-kScreenWidth / 2, kScreenHeight / 2); + self.calendarContainerView.transform = CGAffineTransformMakeScale(0.1, 0.1); + self.monthAndYearPickerView.transform = CGAffineTransformMakeScale(1.0, 1.0); + //self.obscureButtonView.alpha = 0.5; + }completion:^(BOOL finished){ + if (finished) + { + //NSLog(@"%ld", self.yearAndMonthButtonView.year); + //NSLog(@"%ld", [self currentYear]); + [self.monthAndYearPickerView selectRow:self.yearAndMonthButtonView.year - [self currentYear] + 10 inComponent:0 animated:YES]; + [self.monthAndYearPickerView selectRow:self.yearAndMonthButtonView.month - 1 inComponent:1 animated:YES]; + //self.calendarContainerView.hidden = YES; +// [UIView animateWithDuration:0.3 animations:^{ +// self.monthAndYearPickerView.transform = CGAffineTransformMakeScale(1.0, 1.0); +// }completion:^(BOOL finished){ +// if (finished) NSLog(@"%@", NSStringFromCGRect(self.monthAndYearPickerView.frame)); +// }]; + } + }]; +} +//隐藏picker view +- (void)hideMonthAndYearPickerView +{ + NSLog(@"hideMonthAndYearPickerView"); + [self.yearAndMonthButtonView toogleArrowImage]; + isShowingPickerView = NO; + self.calendarContainerView.hidden = NO; + [UIView animateWithDuration:0.3 animations:^{ + self.monthAndYearPickerView.transform = CGAffineTransformMakeScale(0.1, 0.1); + //self.obscureButtonView.alpha = 0.5; + }completion:^(BOOL finished){ + if (finished) + { + self.monthAndYearPickerView.hidden = YES; + [UIView animateWithDuration:0.3 animations:^{ + self.calendarContainerView.transform = CGAffineTransformMakeScale(1.0, 1.0); + //self.calendarContainerView.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2); + }completion:nil]; + } + }]; +} +//点击年月按钮 +- (void)yearAndMonthButtonClicked +{ + if (!isShowingPickerView) + { + NSLog(@"notShowingPickerView"); + [self showMonthAndYearPickerView]; + isShowingPickerView = YES; + } + else + { + NSLog(@"isShowingPickerView"); + [self hideMonthAndYearPickerView]; + isShowingPickerView = NO; + } +} +//返回今天所在年份 +- (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 diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Info.plist" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Info.plist" new file mode 100644 index 0000000..0209678 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Info.plist" @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + + + diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Macro.h" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Macro.h" new file mode 100644 index 0000000..29af7ef --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/Macro.h" @@ -0,0 +1,30 @@ +// +// Macro.h +// 京东金融(仿) +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#ifndef Macro_h +#define Macro_h + +#define kScreenHeight ([UIScreen mainScreen].bounds.size.height) +#define kScreenWidth ([UIScreen mainScreen].bounds.size.width) + +#define backgroundColorWhite [UIColor colorWithWhite:0.93 alpha:1.000] + +//#define weakify(...) \ +//autoreleasepool {} \ +//metamacro_foreach_cxt(ext_weakify_,, __weak, __VA_ARGS__) +// +//#define strongify(...) \ +//autoreleasepool {} \ +//_Pragma("clang diagnostic push") \ +//_Pragma("clang diagnostic ignored \"-Wshadow\"") \ +//metamacro_foreach(ext_strongify_,, __VA_ARGS__) \ +//_Pragma("clang diagnostic pop") + + + +#endif /* Macro_h */ diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/MonthAndYearPickerView.h" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/MonthAndYearPickerView.h" new file mode 100644 index 0000000..481958c --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/MonthAndYearPickerView.h" @@ -0,0 +1,27 @@ +// +// MonthAndYearPickerView.h +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import + +@protocol ActionDelegate + +- (void)didClickOKButton; +- (void)didClickCancelButton; + +@end + +@interface MonthAndYearPickerView : UIView + +@property (nonatomic) id pickerViewDelegate; +@property (nonatomic) id pickerViewDataSource; +@property (nonatomic) idactionDelegate; + +- (NSInteger)selectedRowInComponent:(NSInteger)component; +- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/MonthAndYearPickerView.m" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/MonthAndYearPickerView.m" new file mode 100644 index 0000000..7e24df0 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/MonthAndYearPickerView.m" @@ -0,0 +1,122 @@ +// +// MonthAndYearPickerView.m +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import "MonthAndYearPickerView.h" + +#import "UIView+FrameProcessor.h" +#import "Macro.h" + +@interface MonthAndYearPickerView () + +@property (nonatomic) UIPickerView *monthAndYearPickerView; +@property (nonatomic) UIButton *okButton; +@property (nonatomic) UIButton *cancelButton; + +@end + +@implementation MonthAndYearPickerView + +- (instancetype)init +{ + self = [super init]; + if (self) { + [self configureViews]; + + } + return self; +} + +- (void)configureViews +{ + self.backgroundColor = [UIColor whiteColor]; + [self addSubview:self.monthAndYearPickerView]; + [self addSubview:self.cancelButton]; + [self addSubview:self.okButton]; + + self.monthAndYearPickerView.translatesAutoresizingMaskIntoConstraints = NO; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.monthAndYearPickerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; + [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:@{@"view": self.monthAndYearPickerView}]]; + + + self.cancelButton.translatesAutoresizingMaskIntoConstraints = NO; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.cancelButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:self.size.width * 1.0 / 12]]; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.cancelButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.monthAndYearPickerView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; + + self.okButton.translatesAutoresizingMaskIntoConstraints = NO; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.okButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant: -self.size.width * 1.0 / 12]]; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.okButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.monthAndYearPickerView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; +} + +- (NSInteger)selectedRowInComponent:(NSInteger)component +{ + return [self.monthAndYearPickerView selectedRowInComponent:component]; +} + +- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated +{ + [self.monthAndYearPickerView selectRow:row inComponent:component animated:animated]; +} + +#pragma Accessor methods +- (UIPickerView *)monthAndYearPickerView +{ + if (_monthAndYearPickerView == nil) + { + _monthAndYearPickerView = [[UIPickerView alloc] init]; + } + + return _monthAndYearPickerView; +} + +- (UIButton *)okButton +{ + if (_okButton == nil) + { + _okButton = [UIButton buttonWithType:UIButtonTypeSystem]; + _okButton.backgroundColor = [UIColor clearColor]; + _okButton.titleLabel.font = [UIFont systemFontOfSize:18]; + [_okButton setTitle:@"确认" forState:UIControlStateNormal]; + } + + return _okButton; +} + +- (UIButton *)cancelButton +{ + if (_cancelButton == nil) + { + _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem]; + _cancelButton.backgroundColor = [UIColor clearColor]; + [_cancelButton setTitle:@"取消" forState:UIControlStateNormal]; + _cancelButton.titleLabel.font = [UIFont systemFontOfSize:18]; + _cancelButton.titleLabel.textColor = [UIColor blackColor]; + } + + return _cancelButton; +} + +- (void)setPickerViewDelegate:(id)pickerViewDelegate +{ + _pickerViewDelegate = pickerViewDelegate; + self.monthAndYearPickerView.delegate = pickerViewDelegate; +} + +- (void)setPickerViewDataSource:(id)pickerViewDataSource +{ + _pickerViewDataSource = pickerViewDataSource; + self.monthAndYearPickerView.dataSource = pickerViewDataSource; +} + +- (void)setActionDelegate:(id)actionDelegate +{ + _actionDelegate = actionDelegate; + [self.okButton addTarget:actionDelegate action:@selector(didClickOKButton) forControlEvents:UIControlEventTouchUpInside]; + [self.cancelButton addTarget:actionDelegate action:@selector(didClickCancelButton) forControlEvents:UIControlEventTouchUpInside]; +} + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/UIView+FrameProcessor.h" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/UIView+FrameProcessor.h" new file mode 100644 index 0000000..5f2e64a --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/UIView+FrameProcessor.h" @@ -0,0 +1,20 @@ +// +// UIView+FrameProcessor.h +// 京东金融(仿) +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import + +@interface UIView (FrameProcessor) + +@property CGFloat centerX; +@property CGFloat centerY; +@property CGSize size; +@property CGFloat x; +@property CGFloat y; +@property CGPoint origin; + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/UIView+FrameProcessor.m" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/UIView+FrameProcessor.m" new file mode 100644 index 0000000..a348110 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/UIView+FrameProcessor.m" @@ -0,0 +1,85 @@ +// +// UIView+FrameProcessor.m +// 京东金融(仿) +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import "UIView+FrameProcessor.h" + +@implementation UIView (FrameProcessor) + +- (void)setCenterX:(CGFloat)centerX +{ + CGPoint newCenter = self.center; + newCenter.x = centerX; + self.center = newCenter; +} + +- (CGFloat)centerX +{ + return self.center.x; +} + +- (void)setCenterY:(CGFloat)centerY +{ + CGPoint newCenter = self.center; + newCenter.y = centerY; + self.center = newCenter; +} + +- (CGFloat)centerY +{ + return self.center.y; +} + +- (void)setSize:(CGSize)size +{ + CGRect newBounds = self.bounds; + newBounds.size = size; + self.bounds = newBounds; +} + +- (CGSize)size +{ + return self.bounds.size; +} + +- (void)setX:(CGFloat)x +{ + CGRect newFrame = self.frame; + newFrame.origin.x = x; + self.frame = newFrame; +} + +- (CGFloat)x +{ + return self.frame.origin.x; +} + +- (void)setY:(CGFloat)y +{ + CGRect newFrame = self.frame; + newFrame.origin.y = y; + self.frame = newFrame; +} + +- (CGFloat)y +{ + return self.frame.origin.y; +} + +- (CGPoint)origin +{ + return self.frame.origin; +} + +- (void)setOrigin:(CGPoint)origin +{ + CGRect newFrame = self.frame; + newFrame.origin = origin; + self.frame = newFrame; +} + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/YearAndMonthButtonView.h" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/YearAndMonthButtonView.h" new file mode 100644 index 0000000..2502617 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/YearAndMonthButtonView.h" @@ -0,0 +1,19 @@ +// +// YearAndMonthView.h +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import + +@interface YearAndMonthButtonView : UIButton + +@property (nonatomic)NSInteger year; +@property (nonatomic)NSInteger month; + +- (void)toogleArrowImage; + + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/YearAndMonthButtonView.m" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/YearAndMonthButtonView.m" new file mode 100644 index 0000000..e0bcc58 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/YearAndMonthButtonView.m" @@ -0,0 +1,161 @@ +// +// YearAndMonthView.m +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import "YearAndMonthButtonView.h" +#import "UIView+FrameProcessor.h" + +@interface YearAndMonthButtonView () + +@property (nonatomic)UILabel *yearLabel; +@property (nonatomic)UILabel *monthLabel; +@property (nonatomic)UIImageView *arrowImageView; +@property (nonatomic) NSArray *monthStringArray; + +@end + +@implementation YearAndMonthButtonView + + +- (instancetype)init +{ + self = [super init]; + if (self) { + [self configureViews]; + self.year = [self currentYear]; + self.month = [self currentMonth]; + } + return self; +} + +- (void)configureViews +{ + //self.backgroundColor = [UIColor redColor]; + [self addSubview:self.monthLabel]; + [self addSubview:self.yearLabel]; + [self addSubview:self.arrowImageView]; + + self.monthLabel.translatesAutoresizingMaskIntoConstraints = NO; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.monthLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.monthLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; + + self.yearLabel.translatesAutoresizingMaskIntoConstraints = NO; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.yearLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.monthLabel attribute:NSLayoutAttributeRight multiplier:1 constant:5]]; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.yearLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.monthLabel attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; + + self.arrowImageView.translatesAutoresizingMaskIntoConstraints = NO; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.arrowImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.yearLabel attribute:NSLayoutAttributeRight multiplier:1 constant:0]]; + [self addConstraint:[NSLayoutConstraint constraintWithItem:self.arrowImageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.yearLabel attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]]; +} + +- (CGSize)sizeThatFits:(CGSize)size +{ + [self.yearLabel sizeToFit]; + [self.monthLabel sizeToFit]; + [self.arrowImageView sizeToFit]; + return CGSizeMake(self.yearLabel.size.width + 5 + self.monthLabel.size.width + self.arrowImageView.size.width, self.monthLabel.size.height); +} + +- (void)toogleArrowImage +{ + NSLog(@"toogleArrowImage"); + self.arrowImageView.transform = CGAffineTransformRotate(self.arrowImageView.transform, M_PI); +} + + +#pragma mark - Accessor Methods +- (UILabel *)yearLabel +{ + if (_yearLabel == nil) + { + _yearLabel = [[UILabel alloc] init]; + _yearLabel.font = [UIFont systemFontOfSize:15]; + _yearLabel.backgroundColor = [UIColor clearColor]; + _yearLabel.text = [NSString stringWithFormat:@"%ld", [self currentYear]]; + } + + return _yearLabel; +} + +- (UILabel *)monthLabel +{ + if (_monthLabel == nil) + { + _monthLabel = [[UILabel alloc] init]; + _monthLabel.font = [UIFont systemFontOfSize:25]; + _monthLabel.backgroundColor = [UIColor clearColor]; + _monthLabel.text = self.monthStringArray[[self currentMonth] - 1]; + } + + return _monthLabel; +} + +- (UIImageView *)arrowImageView +{ + if (_arrowImageView == nil) + { + CGFloat length = 8; + UIGraphicsBeginImageContext(CGSizeMake(length, length / 2)); + UIBezierPath *path = [[UIBezierPath alloc] init]; + [path moveToPoint:CGPointMake(0, 0)]; + [path addLineToPoint:CGPointMake(length, 0)]; + [path addLineToPoint:CGPointMake(length / 2, length / 2)]; + [path closePath]; + [[UIColor blackColor] set]; + [path stroke]; + [path fill]; + UIImage *arrowImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + _arrowImageView = [[UIImageView alloc] initWithImage:arrowImage]; + } + + return _arrowImageView; +} + +- (void)setYear:(NSInteger)year +{ + _year = year; + self.yearLabel.text = [NSString stringWithFormat:@"%ld", year]; +} + +- (void)setMonth:(NSInteger)month +{ + _month = month; + self.monthLabel.text = self.monthStringArray[month - 1]; +} + + +- (NSArray *)monthStringArray +{ + if (_monthStringArray == nil) + { + _monthStringArray = @[@"Jan", @"Feb", @"Mar", @"Apr", @"May", @"Jun", @"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec"]; + } + return _monthStringArray; +} + +- (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 diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/main.m" "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/main.m" new file mode 100644 index 0000000..85719c4 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project02/Calendar/Calendar/main.m" @@ -0,0 +1,16 @@ +// +// main.m +// Calendar +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import +#import "AppDelegate.h" + +int main(int argc, char * argv[]) { + @autoreleasepool { + return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); + } +} diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/AppDelegate.h" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/AppDelegate.h" new file mode 100644 index 0000000..099f344 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/AppDelegate.h" @@ -0,0 +1,17 @@ +// +// AppDelegate.h +// TaskList +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import + +@interface AppDelegate : UIResponder + +@property (strong, nonatomic) UIWindow *window; + + +@end + diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/AppDelegate.m" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/AppDelegate.m" new file mode 100644 index 0000000..94d0afb --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/AppDelegate.m" @@ -0,0 +1,68 @@ +// +// AppDelegate.m +// TaskList +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import "AppDelegate.h" +#import "TaskListTableViewController.h" +#import "TaskListModel.h" + +@interface AppDelegate () + +@end + +@implementation AppDelegate +{ + TaskListModel *taskListModel; +} + + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + // Override point for customization after application launch. + self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; + TaskListTableViewController *taskListVC = [[TaskListTableViewController alloc] init]; + + taskListModel = [[TaskListModel alloc] init]; + [taskListModel loadTaskList];//载入存储的数据 + taskListVC.taskList = taskListModel; + + UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:taskListVC]; + self.window.rootViewController = rootNav; + [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:. + [taskListModel saveTaskList];//保存数据 +} + + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Assets.xcassets/AppIcon.appiconset/Contents.json" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Assets.xcassets/AppIcon.appiconset/Contents.json" new file mode 100644 index 0000000..b8236c6 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Assets.xcassets/AppIcon.appiconset/Contents.json" @@ -0,0 +1,48 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Base.lproj/LaunchScreen.storyboard" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Base.lproj/LaunchScreen.storyboard" new file mode 100644 index 0000000..fdf3f97 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Base.lproj/LaunchScreen.storyboard" @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Info.plist" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Info.plist" new file mode 100644 index 0000000..b8901ee --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/Info.plist" @@ -0,0 +1,36 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListModel.h" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListModel.h" new file mode 100644 index 0000000..7b0308f --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListModel.h" @@ -0,0 +1,26 @@ +// +// TaskListModel.h +// TaskList +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import + +@protocol ListModel + +- (NSInteger)count; +- (id)objectAtIndex:(NSInteger)index; +- (void)insertObject:(id)object AtIndex:(NSInteger)index; +- (void)removeObjectAtIndex:(NSInteger)index; + +@end + +@interface TaskListModel : NSObject + +//- (instancetype)initWithArray:(NSArray *)array; +- (void)saveTaskList; +- (void)loadTaskList; + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListModel.m" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListModel.m" new file mode 100644 index 0000000..b41ede3 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListModel.m" @@ -0,0 +1,85 @@ +// +// TaskListModel.m +// TaskList +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import "TaskListModel.h" + +@interface TaskListModel() + +@property (nonatomic) NSMutableArray *taskArray; + +@end + +static NSString * const StorageKey = @"TaskList"; + +@implementation TaskListModel + + +#pragma mark - ListModel Protocol +- (id)objectAtIndex:(NSInteger)index +{ + return self.taskArray[index]; +} + +- (void)insertObject:(id)object AtIndex:(NSInteger)index +{ + [self.taskArray insertObject:object atIndex:index]; +} + +- (void)removeObjectAtIndex:(NSInteger)index +{ + [self.taskArray removeObjectAtIndex:index]; +} + +#pragma mark - Accessor Methods +- (NSMutableArray *)taskArray +{ + if (_taskArray == nil) + { + _taskArray = [[NSMutableArray alloc] init]; + } + + return _taskArray; +} + +- (NSInteger)count +{ + return self.taskArray.count; +} + +# pragma mark - persistent storage +- (NSString *)dataFilePath +{ + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + return [paths[0] stringByAppendingPathComponent:@"TaskList.plist"]; +} + +- (void)saveTaskList +{ + NSMutableData *data = [NSMutableData data]; + NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; + [archiver encodeObject:self.taskArray forKey:StorageKey]; + [archiver finishEncoding]; + [data writeToFile:[self dataFilePath] atomically:YES]; +} + +- (void)loadTaskList +{ + NSString *path = [self dataFilePath]; + if ([[NSFileManager defaultManager] fileExistsAtPath:path]) + { + NSData *data = [NSData dataWithContentsOfFile:path]; + if (data != nil) + { + NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; + self.taskArray = [[NSMutableArray alloc] initWithArray:((NSArray *)[unarchiver decodeObjectForKey:StorageKey])]; + [unarchiver finishDecoding]; + } + } +} + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListTableViewController.h" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListTableViewController.h" new file mode 100644 index 0000000..f89ff1c --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListTableViewController.h" @@ -0,0 +1,16 @@ +// +// TaskListTableViewController.h +// TaskList +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import +#import "TaskListModel.h" + +@interface TaskListTableViewController : UITableViewController + +@property (nonatomic) id taskList; + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListTableViewController.m" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListTableViewController.m" new file mode 100644 index 0000000..b6f48c1 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/TaskListTableViewController.m" @@ -0,0 +1,300 @@ +// +// TaskListTableViewController.m +// TaskList +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import "TaskListTableViewController.h" +//#import "TaskListModel.h" + +@interface TaskListTableViewController () + +@property (nonatomic) UITextField *taskTextField;//输入框 +@property (nonatomic) UIBarButtonItem *insertBarItem;//添加任务按钮 +@property (nonatomic) UIBarButtonItem *startEditingBarItem;//编辑cell按钮,可删除或移动cell +@property (nonatomic) UIBarButtonItem *endEditingBarItem;//结束编辑cell按钮 + +@property (nonatomic) UIButton *obscureButtonView;//在键盘外点击退出键盘 + + + +@end + +static NSString * const CellIdentifier = @"TaskCellIdentifier"; + + +@implementation TaskListTableViewController +{ + //UILongPressGestureRecognizer *longPressGestureRecognizer; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + + [self configureViews]; +} + +- (void)configureViews +{ + self.navigationItem.titleView = self.taskTextField; + self.navigationItem.rightBarButtonItem = self.startEditingBarItem; +} + +//- (void)configureGestures +//{ +// longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(startEditing)]; +// [self.tableView addGestureRecognizer:longPressGestureRecognizer]; +//} + +- (void)hideKeyBoard +{ + [self.taskTextField resignFirstResponder]; + if ([self.taskTextField.text isEqualToString:@""]) self.navigationItem.rightBarButtonItem = self.startEditingBarItem; + //self.insertBarItem.enabled = NO; + [UIView animateWithDuration:0.3 animations:^{ + self.obscureButtonView.alpha = 0; + CGPointMake(self.view.bounds.size.width / 2, -self.view.bounds.size.height / 2); + }completion:^(BOOL finished){ + self.obscureButtonView.hidden = YES; + }]; +} +//添加新的任务 +- (void)addTask +{ + [self.taskList insertObject:self.taskTextField.text AtIndex:0]; + [self hideKeyBoard]; + self.insertBarItem.enabled = NO; + self.taskTextField.text = @""; + self.navigationItem.rightBarButtonItem = self.startEditingBarItem; + + [self.tableView reloadData]; +} +//开始编辑cell,将编辑按钮换为结束按钮 +- (void)startEditing +{ + NSLog(@"enterEditing"); + self.taskTextField.enabled = NO; + [self.tableView setEditing:YES animated:YES]; + self.navigationItem.rightBarButtonItem = self.endEditingBarItem; + + //longPressGestureRecognizer.enabled = NO; +} +//结束编辑cell,退出tableview的编辑模式,将结束按钮换为编辑按钮 +- (void)endEditing +{ + self.taskTextField.enabled = YES; + self.navigationItem.rightBarButtonItem = self.startEditingBarItem; + [self.tableView setEditing:NO animated:YES]; + //longPressGestureRecognizer.enabled = YES; +} + +#pragma mark - Table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + + return [self.taskList count]; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; + if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; + cell.textLabel.text = (NSString *)[self.taskList objectAtIndex:indexPath.row]; + + return cell; +} + +//向左滑动,提供删除按钮 +- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath +{ + return YES; +} + +- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath +{ + if (editingStyle == UITableViewCellEditingStyleDelete) + { + [self.taskList removeObjectAtIndex:indexPath.row]; + [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; + } + else if (editingStyle == UITableViewCellEditingStyleInsert) + { + [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; + } +} +//移动cell位置 +- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath +{ + return YES; +} + +- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath +{ + id object = [self.taskList objectAtIndex:sourceIndexPath.row]; + [self.taskList removeObjectAtIndex:sourceIndexPath.row]; + [self.taskList insertObject: object AtIndex: destinationIndexPath.row]; +} + + +#pragma mark - Table view delegate + +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath +{ + return 60; +} + + +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + [tableView deselectRowAtIndexPath:indexPath animated:NO]; +} + +//在一个cell从tableview中移除后,如果taskList为空,则disable编辑按钮 +- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath +{ + if ([self.taskList count] == 0) + { + self.startEditingBarItem.enabled = NO; + } +} +//在一个cell将要被展示时,若taskList不为空,则enable编辑按钮 +- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath +{ + if ([self.taskList count] != 0) + { + self.startEditingBarItem.enabled = YES; + } +} + +#pragma UIScrollViewDelegate + +//- (void)scrollViewDidScroll:(UIScrollView *)scrollView +//{ +// [self hideKeyBoard]; +//} + +#pragma mark - UITextFieldDelegate +//用户输入文字时,将编辑按钮换为添加按钮 +- (void)textFieldDidBeginEditing:(UITextField *)textField +{ + self.navigationItem.rightBarButtonItem = self.insertBarItem; +} +//监听textField的变化,没有文字就disable添加按钮,有文字就enable添加按钮 +- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string +{ + //NSLog(@"shouldChangeCharactersInRange"); +// NSLog(@"textField.text: %@", textField.text); +// NSLog(@"string: %@", string); + NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; + //NSLog(@"new string: %@", newString); + if (![newString isEqual: @""]) self.insertBarItem.enabled = YES; + else self.insertBarItem.enabled = NO; + + return YES; +} +//用户点击textField内嵌的清除按钮 +- (BOOL)textFieldShouldClear:(UITextField *)textField +{ + //NSLog(@"textFieldShouldClear"); + textField.text = @""; + self.insertBarItem.enabled = NO; + + return NO;; +} +//键盘弹出时,一起弹出obscureButtonView,点击obscureButtonView可以退出编辑,隐藏键盘 +- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField +{ + self.obscureButtonView.hidden = NO; + [UIView animateWithDuration:0.3 animations:^{ + self.obscureButtonView.alpha = 0.5; + self.obscureButtonView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2); + }completion:nil]; + + return YES; +} + +#pragma mark - Accessor methods + +- (UITextField *)taskTextField +{ + if (_taskTextField == nil) + { + _taskTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * 3.0 / 4, 30)]; + _taskTextField.backgroundColor = [UIColor whiteColor]; + _taskTextField.placeholder = @"Type in a new task here"; + _taskTextField.autoresizingMask = UIViewAutoresizingNone; + _taskTextField.borderStyle = UITextBorderStyleRoundedRect; + _taskTextField.clearButtonMode = UITextFieldViewModeWhileEditing; + + _taskTextField.delegate = self; + } + + return _taskTextField; +} + +- (UIBarButtonItem *)insertBarItem +{ + if (_insertBarItem == nil) + { + _insertBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTask)]; + _insertBarItem.enabled = NO; + } + + return _insertBarItem; +} + +- (UIBarButtonItem *)startEditingBarItem +{ + if (_startEditingBarItem == nil) + { + _startEditingBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(startEditing)]; + if ([self.taskList count] == 0) _startEditingBarItem.enabled = NO; + } + + return _startEditingBarItem; +} + +- (UIBarButtonItem *)endEditingBarItem +{ + if (_endEditingBarItem == nil) + { + _endEditingBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(endEditing)]; + } + + return _endEditingBarItem; +} + +- (UIButton *)obscureButtonView +{ + if (_obscureButtonView == nil) + { + _obscureButtonView = [[UIButton alloc] initWithFrame:self.view.bounds]; + _obscureButtonView.backgroundColor = [UIColor blackColor]; + _obscureButtonView.alpha = 0; + _obscureButtonView.hidden = YES; + [_obscureButtonView addTarget:self action:@selector(hideKeyBoard) forControlEvents:UIControlEventTouchUpInside]; + [self.view addSubview:_obscureButtonView]; + _obscureButtonView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height); + } + + return _obscureButtonView; +} + +- (id)taskList +{ + if (_taskList == nil) + { + _taskList = [[TaskListModel alloc] init]; + } + return _taskList; +} + + + +@end diff --git "a/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/main.m" "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/main.m" new file mode 100644 index 0000000..50f9952 --- /dev/null +++ "b/21651012\345\260\271\347\233\212\351\271\217/project03/TaskList/TaskList/main.m" @@ -0,0 +1,16 @@ +// +// main.m +// TaskList +// +// Created by yinyipeng on 16/11/10. +// Copyright © 2016年 snailset. All rights reserved. +// + +#import +#import "AppDelegate.h" + +int main(int argc, char * argv[]) { + @autoreleasepool { + return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); + } +}