From 27ce88cfcb6e0ae1e70e6ce0f0a3504805267cb4 Mon Sep 17 00:00:00 2001 From: DingWenchao Date: Mon, 13 Jul 2015 13:16:48 +0800 Subject: [PATCH 1/4] Fix usage in childViewController Fix usage in childViewController --- Pod/Classes/FSCalendar.m | 37 ++++++++++++++-------------------- Pod/Classes/FSCalendarHeader.m | 24 +++++++++++++++++----- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/Pod/Classes/FSCalendar.m b/Pod/Classes/FSCalendar.m index 1f106a0..8cd454e 100644 --- a/Pod/Classes/FSCalendar.m +++ b/Pod/Classes/FSCalendar.m @@ -48,11 +48,14 @@ @property (strong, nonatomic) NSCalendar *calendar; @property (assign, nonatomic) BOOL supressEvent; +@property (assign, nonatomic) BOOL needsAdjustingMonthPosition; + - (void)orientationDidChange:(NSNotification *)notification; - (NSDate *)dateForIndexPath:(NSIndexPath *)indexPath; - (NSIndexPath *)indexPathForDate:(NSDate *)date; +- (void)setNeedsAdjusting; - (void)scrollToDate:(NSDate *)date; - (void)scrollToDate:(NSDate *)date animate:(BOOL)animate; @@ -155,19 +158,6 @@ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; - dispatch_async(dispatch_get_main_queue(), ^{ - - if (!_selectedDate) { - _supressEvent = YES; - NSDate *today = [NSDate date].fs_dateByIgnoringTimeComponents; - if ([self isDateInRange:today]) { - self.selectedDate = today; - } - _supressEvent = NO; - } - - }); - } - (void)dealloc @@ -203,6 +193,11 @@ }]; [_appearance adjustTitleIfNecessary]; + if (_needsAdjustingMonthPosition) { + _needsAdjustingMonthPosition = NO; + self.selectedDate = _selectedDate ?: [NSDate date]; + } + _supressEvent = NO; } @@ -220,15 +215,7 @@ { [super didMoveToWindow]; if (self.window) { - // 防止Push到其他的控制器中,旋转手机再返回 - // In case : Pushing to another view controller then change orientation then pop back - [self setNeedsLayout]; - dispatch_async(dispatch_get_main_queue(), ^{ - if (_currentMonth) { - [self scrollToDate:_currentMonth]; - [_collectionView.visibleCells makeObjectsPerformSelector:@selector(setNeedsLayout)]; - } - }); + [self setNeedsAdjusting]; } } @@ -506,6 +493,12 @@ #pragma mark - Private +- (void)setNeedsAdjusting +{ + _needsAdjustingMonthPosition = YES; + [self setNeedsLayout]; +} + - (void)scrollToDate:(NSDate *)date { [self scrollToDate:date animate:NO]; diff --git a/Pod/Classes/FSCalendarHeader.m b/Pod/Classes/FSCalendarHeader.m index dae8624..18714ae 100644 --- a/Pod/Classes/FSCalendarHeader.m +++ b/Pod/Classes/FSCalendarHeader.m @@ -19,8 +19,12 @@ @property (weak, nonatomic) UICollectionView *collectionView; @property (weak, nonatomic) UICollectionViewFlowLayout *collectionViewFlowLayout; +@property (assign, nonatomic) BOOL needsAdjustingMonthPosition; + @property (readonly, nonatomic) FSCalendar *calendar; +- (void)setNeedsAdjusting; + @end @implementation FSCalendarHeader @@ -76,6 +80,14 @@ _collectionView.contentInset = UIEdgeInsetsZero; _collectionViewFlowLayout.itemSize = CGSizeMake(_collectionView.fs_width * 0.5, _collectionView.fs_height); + if (_needsAdjustingMonthPosition) { + _needsAdjustingMonthPosition = NO; + if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) { + _collectionView.contentOffset = CGPointMake((_scrollOffset+0.5)*_collectionViewFlowLayout.itemSize.width, 0); + } else { + _collectionView.contentOffset = CGPointMake(0, _scrollOffset * _collectionViewFlowLayout.itemSize.height); + } + } } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView @@ -132,11 +144,7 @@ if (_scrollOffset != scrollOffset) { _scrollOffset = scrollOffset; } - if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) { - _collectionView.contentOffset = CGPointMake((_scrollOffset+0.5)*_collectionViewFlowLayout.itemSize.width, 0); - } else { - _collectionView.contentOffset = CGPointMake(0, _scrollOffset * _collectionViewFlowLayout.itemSize.height); - } + [self setNeedsAdjusting]; } - (void)setScrollDirection:(UICollectionViewScrollDirection)scrollDirection @@ -172,6 +180,12 @@ return (FSCalendar *)self.superview; } +- (void)setNeedsAdjusting +{ + _needsAdjustingMonthPosition = YES; + [self setNeedsLayout]; +} + @end From 9678b5dba0315380e004dc875b9857c43d8b70f6 Mon Sep 17 00:00:00 2001 From: DingWenchao Date: Mon, 13 Jul 2015 14:11:24 +0800 Subject: [PATCH 2/4] Scroll to currentMonth,not selectedDate Scroll to currentMonth,not selectedDate --- Pod/Classes/FSCalendar.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Pod/Classes/FSCalendar.m b/Pod/Classes/FSCalendar.m index 8cd454e..58e2696 100644 --- a/Pod/Classes/FSCalendar.m +++ b/Pod/Classes/FSCalendar.m @@ -195,7 +195,11 @@ if (_needsAdjustingMonthPosition) { _needsAdjustingMonthPosition = NO; - self.selectedDate = _selectedDate ?: [NSDate date]; + if (!_selectedDate) { + self.selectedDate = [NSDate date]; + } else { + [self scrollToDate:_currentMonth]; + } } _supressEvent = NO; From 379967ea3123dda5e07679dfc5d81c97da39cfd4 Mon Sep 17 00:00:00 2001 From: DingWenchao Date: Mon, 13 Jul 2015 17:41:25 +0800 Subject: [PATCH 3/4] Update header dynamicly Update header dynamicly --- Pod/Classes/FSCalendar.m | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Pod/Classes/FSCalendar.m b/Pod/Classes/FSCalendar.m index 58e2696..2ab690d 100644 --- a/Pod/Classes/FSCalendar.m +++ b/Pod/Classes/FSCalendar.m @@ -170,9 +170,7 @@ [super layoutSubviews]; _supressEvent = YES; CGFloat padding = self.fs_height * 0.01; - if (_headerHeight == -1) { - _header.frame = CGRectMake(0, 0, self.fs_width, kDefaultHeaderHeight); - } + _header.frame = CGRectMake(0, 0, self.fs_width, _headerHeight == -1 ? kDefaultHeaderHeight : _headerHeight); _collectionView.frame = CGRectMake(0, kWeekHeight+_header.fs_height, self.fs_width, self.fs_height-kWeekHeight-_header.fs_height); _collectionView.contentInset = UIEdgeInsetsZero; From e300387712bb7c252629065d2d146d73ce366e16 Mon Sep 17 00:00:00 2001 From: DingWenchao Date: Sat, 18 Jul 2015 08:51:40 +0800 Subject: [PATCH 4/4] Update readme Update readme --- README.md | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/README.md b/README.md index 157b93a..04cd5f0 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,8 @@ calendar.appearance.cellStyle = .Rectangle #### `FSCalendar` can show subtitle for each day +* Objective - c + ```objective-c // FSCalendarDataSource - (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date @@ -140,12 +142,23 @@ calendar.appearance.cellStyle = .Rectangle } ``` +* Swift + +```swift +// FSCalendarDataSource +func calendar(calendar: FSCalendar!, subtitleForDate date: NSDate!) -> String! { + return yourSubtitle +} +``` + ![fscalendar---subtitle2](https://cloud.githubusercontent.com/assets/5186464/8449075/b0bb34ee-2000-11e5-9c4a-401bc708d9ea.png)
![fscalendar---subtitle1](https://cloud.githubusercontent.com/assets/5186464/8449076/b0be3d88-2000-11e5-9c5d-22ecd325b6cc.png) #### And event dot for some days +* Objective - c + ```objective-c // FSCalendarDataSource - (BOOL)calendar:(FSCalendar *)calendar hasEventForDate:(NSDate *)date @@ -154,8 +167,19 @@ calendar.appearance.cellStyle = .Rectangle } ``` +* Swift + +```swift +// FSCalendarDataSource +func calendar(calendar: FSCalendar!, hasEventForDate date: NSDate!) -> Bool { + return shouldShowEventDot +} +``` + #### Or image for some days +* Objective - c + ```objective-c // FSCalendarDataSource - (UIImage *)calendar:(FSCalendar *)calendar imageForDate:(NSDate *)date @@ -164,6 +188,15 @@ calendar.appearance.cellStyle = .Rectangle } ``` +* Swift + +```swift +// FSCalendarDataSource +func calendar(calendar: FSCalendar!, imageForDate date: NSDate!) -> UIImage! { + return anyImage +} +``` + ![fscalendar---image](https://cloud.githubusercontent.com/assets/5186464/8449772/e94d3126-2006-11e5-8871-e4f8dbce81ea.png) #### There are left and right boundaries @@ -182,6 +215,9 @@ calendar.appearance.cellStyle = .Rectangle ``` #### You can do something when a date is selected + +* Objective - c + ```objective-c // FSCalendarDelegate - (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date @@ -190,7 +226,20 @@ calendar.appearance.cellStyle = .Rectangle } ``` +* Swift + +```swift +// FSCalendarDelegate +func calendar(calendar: FSCalendar!, didSelectDate date: NSDate!) { + +} + +``` + #### You can prevent it from being selected + +* Objective - c + ```objective-c // FSCalendarDelegate - (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date @@ -202,7 +251,22 @@ calendar.appearance.cellStyle = .Rectangle } ``` +* Swift + +```swift +func calendar(calendar: FSCalendar!, shouldSelectDate date: NSDate!) -> Bool { + if dateShouldNotBeSelected { + return false + } + return true +} +``` + + #### You will get notified when `FSCalendar` changes the month + +* Objective - c + ```objective-c - (void)calendarCurrentMonthDidChange:(FSCalendar *)calendar { @@ -210,6 +274,14 @@ calendar.appearance.cellStyle = .Rectangle } ``` +* Swift + +```swift +func calendarCurrentMonthDidChange(calendar: FSCalendar!) { + // Do something +} +``` + #### `FSCalendar` can be used on `iPad`. ![fscalendar-ipad](https://cloud.githubusercontent.com/assets/5186464/6502151/b4ce3092-c35b-11e4-827a-498d73579d78.jpg)