FSCalendar/Example-Objc/StoryboardExampleViewContro...

291 lines
10 KiB
Objective-C

//
// StoryboardExampleViewController.m
// Chinese-Lunar-Calendar
//
// Created by Wenchao Ding on 01/29/2015.
// Copyright (c) 2014 Wenchao Ding. All rights reserved.
//
#import "StoryboardExampleViewController.h"
#import "CalendarConfigViewController.h"
@interface StoryboardExampleViewController ()
@property (strong, nonatomic) NSCalendar *gregorianCalendar;
@property (strong, nonatomic) NSCalendar *lunarCalendar;
@property (strong, nonatomic) NSArray *lunarChars;
@property (strong, nonatomic) NSDateFormatter *dateFormatter1;
@property (strong, nonatomic) NSDateFormatter *dateFormatter2;
@end
@implementation StoryboardExampleViewController
#pragma mark - Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
self.gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSLocale *chinese = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
self.dateFormatter1 = [[NSDateFormatter alloc] init];
self.dateFormatter1.locale = chinese;
self.dateFormatter1.dateFormat = @"yyyy/MM/dd";
self.dateFormatter2 = [[NSDateFormatter alloc] init];
self.dateFormatter2.locale = chinese;
self.dateFormatter2.dateFormat = @"yyyy-MM-dd";
self.lunarCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
self.lunarCalendar.locale = chinese;
self.lunarChars = @[@"初一",@"初二",@"初三",@"初四",@"初五",@"初六",@"初七",@"初八",@"初九",@"初十",@"十一",@"十二",@"十三",@"十四",@"十五",@"十六",@"十七",@"十八",@"十九",@"二十",@"二一",@"二二",@"二三",@"二四",@"二五",@"二六",@"二七",@"二八",@"二九",@"三十"];
_scrollDirection = _calendar.scrollDirection;
_calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase|FSCalendarCaseOptionsWeekdayUsesUpperCase;
// [_calendar selectDate:[self.dateFormatter1 dateFromString:@"2015/10/05"]];
_datesShouldNotBeSelected = @[@"2016/08/07",
@"2016/09/07",
@"2016/10/07",
@"2016/11/07",
@"2016/12/07",
@"2016/01/07",
@"2016/02/07"];
_datesWithEvent = @[@"2016-10-03",
@"2016-10-07",
@"2016-10-15",
@"2016-10-25"];
// self.calendar.appearance.weekdayBackground = [UIColor cyanColor];
// _calendar.locale = [NSLocale currentLocale];
// Uncomment this to test the month->week & week->month transition
/*
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_calendar setScope:FSCalendarScopeWeek animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_calendar setScope:FSCalendarScopeMonth animated:YES];
});
});
*/
}
- (void)dealloc
{
NSLog(@"%s",__FUNCTION__);
}
#pragma mark - FSCalendarDataSource
- (NSString *)calendar:(FSCalendar *)calendar titleForDate:(NSDate *)date
{
return [self.gregorianCalendar isDateInToday:date] ? @"今天" : nil;
}
- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date
{
if (!_lunar) {
return nil;
}
NSInteger day = [_lunarCalendar component:NSCalendarUnitDay fromDate:date];
return _lunarChars[day-1];
}
- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date
{
return [_datesWithEvent containsObject:[self.dateFormatter2 stringFromDate:date]];
}
- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
{
return [self.dateFormatter1 dateFromString:@"2016/02/01"];
}
- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
{
return [self.dateFormatter1 dateFromString:@"2019/05/31"];
}
#pragma mark - FSCalendarDelegate
- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
BOOL shouldSelect = ![_datesShouldNotBeSelected containsObject:[self.dateFormatter1 stringFromDate:date]];
if (!shouldSelect) {
[[[UIAlertView alloc] initWithTitle:@"FSCalendar"
message:[NSString stringWithFormat:@"FSCalendar delegate forbid %@ to be selected",[self.dateFormatter1 stringFromDate:date]]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil] show];
} else {
NSLog(@"Should select date %@",[self.dateFormatter1 stringFromDate:date]);
}
return shouldSelect;
}
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
NSLog(@"did select date %@",[self.dateFormatter1 stringFromDate:date]);
CGRect frame = [self.calendar frameForDate:date];
NSLog(@"%@",NSStringFromCGRect(frame));
}
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar
{
NSLog(@"did change to page %@",[self.dateFormatter1 stringFromDate:calendar.currentPage]);
}
- (void)calendarCurrentScopeWillChange:(FSCalendar *)calendar animated:(BOOL)animated
{
_calendarHeightConstraint.constant = [calendar sizeThatFits:CGSizeZero].height;
[self.view layoutIfNeeded];
}
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
_calendarHeightConstraint.constant = CGRectGetHeight(bounds);
[self.view layoutIfNeeded];
}
- (CGPoint)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleOffsetForDate:(NSDate *)date
{
if ([self calendar:calendar subtitleForDate:date]) {
return CGPointZero;
}
if ([_datesWithEvent containsObject:[self.dateFormatter2 stringFromDate:date]]) {
return CGPointMake(0, -2);
}
return CGPointZero;
}
- (CGPoint)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventOffsetForDate:(NSDate *)date
{
if ([self calendar:calendar subtitleForDate:date]) {
return CGPointZero;
}
if ([_datesWithEvent containsObject:[self.dateFormatter2 stringFromDate:date]]) {
return CGPointMake(0, -10);
}
return CGPointZero;
}
- (NSArray<UIColor *> *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventSelectionColorsForDate:(nonnull NSDate *)date
{
if ([self calendar:calendar subtitleForDate:date]) {
return @[appearance.eventDefaultColor];
}
if ([_datesWithEvent containsObject:[self.dateFormatter2 stringFromDate:date]]) {
return @[[UIColor whiteColor]];
}
return nil;
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isKindOfClass:[CalendarConfigViewController class]]) {
[segue.destinationViewController setValue:self forKey:@"viewController"];
}
}
#pragma mark - Setter
- (void)setTheme:(NSInteger)theme
{
if (_theme != theme) {
_theme = theme;
switch (theme) {
case 0: {
_calendar.appearance.weekdayTextColor = FSCalendarStandardTitleTextColor;
_calendar.appearance.headerTitleColor = FSCalendarStandardTitleTextColor;
_calendar.appearance.eventDefaultColor = FSCalendarStandardEventDotColor;
_calendar.appearance.selectionColor = FSCalendarStandardSelectionColor;
_calendar.appearance.headerDateFormat = @"MMMM yyyy";
_calendar.appearance.todayColor = FSCalendarStandardTodayColor;
_calendar.appearance.borderRadius = 1.0;
_calendar.appearance.headerMinimumDissolvedAlpha = 0.2;
break;
}
case 1: {
_calendar.appearance.weekdayTextColor = [UIColor redColor];
_calendar.appearance.headerTitleColor = [UIColor darkGrayColor];
_calendar.appearance.eventDefaultColor = [UIColor greenColor];
_calendar.appearance.selectionColor = [UIColor blueColor];
_calendar.appearance.headerDateFormat = @"yyyy-MM";
_calendar.appearance.todayColor = [UIColor redColor];
_calendar.appearance.borderRadius = 1.0;
_calendar.appearance.headerMinimumDissolvedAlpha = 0.0;
break;
}
case 2: {
_calendar.appearance.weekdayTextColor = [UIColor redColor];
_calendar.appearance.headerTitleColor = [UIColor redColor];
_calendar.appearance.eventDefaultColor = [UIColor greenColor];
_calendar.appearance.selectionColor = [UIColor blueColor];
_calendar.appearance.headerDateFormat = @"yyyy/MM";
_calendar.appearance.todayColor = [UIColor orangeColor];
_calendar.appearance.borderRadius = 0;
_calendar.appearance.headerMinimumDissolvedAlpha = 1.0;
break;
}
default:
break;
}
}
}
- (void)setLunar:(BOOL)lunar
{
if (_lunar != lunar) {
_lunar = lunar;
[_calendar reloadData];
}
}
- (void)setScrollDirection:(FSCalendarScrollDirection)scrollDirection
{
if (_scrollDirection != scrollDirection) {
_scrollDirection = scrollDirection;
_calendar.scrollDirection = scrollDirection;
[[[UIAlertView alloc] initWithTitle:@"FSCalendar"
message:[NSString stringWithFormat:@"Now swipe %@",@[@"Vertically", @"Horizontally"][_calendar.scrollDirection]]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil] show];
}
}
- (void)setSelectedDate:(NSDate *)selectedDate
{
[_calendar selectDate:selectedDate];
}
- (void)setFirstWeekday:(NSUInteger)firstWeekday
{
if (_firstWeekday != firstWeekday) {
_firstWeekday = firstWeekday;
_calendar.firstWeekday = firstWeekday;
}
}
@end