Files
FSCalendar/Example/MultipleSelectionViewController.m
T
dingwenchao 131bba5677 Fix crash while frequently changing scope
1. Fix crash while frequently changing scope
2. Fix wrong cell layout while changing orientation under autoLayout
3. Example support RTL
4. Spelling check
2016-02-25 11:35:02 +08:00

103 lines
3.4 KiB
Objective-C

//
// MultipleSelectionViewController.m
// FSCalendar
//
// Created by dingwenchao on 9/9/15.
// Copyright (c) 2015 wenchaoios. All rights reserved.
//
#import "MultipleSelectionViewController.h"
@implementation MultipleSelectionViewController
- (instancetype)init
{
self = [super init];
if (self) {
self.title = @"FSCalendar";
}
return self;
}
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.view = view;
CGFloat height = [[UIDevice currentDevice].model hasPrefix:@"iPad"] ? 450 : 300;
FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), view.bounds.size.width, height)];
calendar.dataSource = self;
calendar.delegate = self;
calendar.backgroundColor = [UIColor whiteColor];
calendar.allowsMultipleSelection = YES;
[self.view addSubview:calendar];
self.calendar = calendar;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[_calendar selectDate:[_calendar dateByAddingDays:1 toDate:[NSDate date]]];
#if 0
FSCalendarTestSelectDate
#endif
}
- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date
{
BOOL shouldDedeselect = [_calendar dayOfDate:date] != 5;
if (!shouldDedeselect) {
[[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Forbidden date %@ to be selected",[calendar stringFromDate:date]] message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
return NO;
}
return YES;
}
- (BOOL)calendar:(FSCalendar *)calendar shouldDeselectDate:(NSDate *)date
{
BOOL shouldDedeselect = [_calendar dayOfDate:date] != 7;
if (!shouldDedeselect) {
[[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Forbidden date %@ to be deselected",[calendar stringFromDate:date]] message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
return NO;
}
return YES;
}
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date
{
NSMutableArray *selectedDates = [NSMutableArray arrayWithCapacity:calendar.selectedDates.count];
[calendar.selectedDates enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[selectedDates addObject:[calendar stringFromDate:obj format:@"yyyy/MM/dd"]];
}];
NSLog(@"selected dates is %@",selectedDates);
}
- (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date
{
NSMutableArray *selectedDates = [NSMutableArray arrayWithCapacity:calendar.selectedDates.count];
[calendar.selectedDates enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[selectedDates addObject:[calendar stringFromDate:obj format:@"yyyy/MM/dd"]];
}];
NSLog(@"selected dates is %@",selectedDates);
}
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance selectionColorForDate:(NSDate *)date
{
if ([_calendar dayOfDate:date] % 2 == 0) {
return appearance.selectionColor;
}
return [UIColor purpleColor];
}
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderDefaultColorForDate:(NSDate *)date
{
if ([@[@17,@18,@19] containsObject:@([calendar dayOfDate:date])]) {
return [UIColor magentaColor];
}
return appearance.borderDefaultColor;
}
@end