diff --git a/Pod/Classes/FSCalendar.m b/Pod/Classes/FSCalendar.m index 1f106a0..2ab690d 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 @@ -180,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; @@ -203,6 +191,15 @@ }]; [_appearance adjustTitleIfNecessary]; + if (_needsAdjustingMonthPosition) { + _needsAdjustingMonthPosition = NO; + if (!_selectedDate) { + self.selectedDate = [NSDate date]; + } else { + [self scrollToDate:_currentMonth]; + } + } + _supressEvent = NO; } @@ -220,15 +217,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 +495,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 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)