diff --git a/Example/FSCalendar/ViewController.m b/Example/FSCalendar/ViewController.m index 670a5ee..0ddd4f4 100644 --- a/Example/FSCalendar/ViewController.m +++ b/Example/FSCalendar/ViewController.m @@ -31,6 +31,11 @@ [super viewDidLoad]; _currentCalendar = [NSCalendar currentCalendar]; _flow = _calendar.flow; + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + _calendar.selectedDate = [[NSDate date] fs_dateByAddingMonths:1]; + }); + } #pragma mark - FSCalendarDataSource diff --git a/Pod/Classes/FSCalendar.h b/Pod/Classes/FSCalendar.h index 051ab30..9e16fa4 100644 --- a/Pod/Classes/FSCalendar.h +++ b/Pod/Classes/FSCalendar.h @@ -60,8 +60,8 @@ typedef NS_OPTIONS(NSInteger, FSCalendarCellState) { @property (weak, nonatomic) IBOutlet FSCalendarHeader *header; @property (copy, nonatomic) NSDate *currentDate; +@property (copy, nonatomic) NSDate *selectedDate; @property (readonly, nonatomic) NSDate *currentMonth; -@property (readonly, nonatomic) NSDate *selectedDate; @property (assign, nonatomic) FSCalendarFlow flow; @property (assign, nonatomic) FSCalendarCellStyle cellStyle UI_APPEARANCE_SELECTOR; diff --git a/Pod/Classes/FSCalendar.m b/Pod/Classes/FSCalendar.m index 85182f1..c73022b 100644 --- a/Pod/Classes/FSCalendar.m +++ b/Pod/Classes/FSCalendar.m @@ -39,7 +39,7 @@ @property (strong, nonatomic) NSMutableArray *weekdays; @property (strong, nonatomic) NSDate *currentMonth; -@property (strong, nonatomic) NSDate *selectedDate; +//@property (strong, nonatomic) NSDate *selectedDate; @property (strong, nonatomic) NSMutableDictionary *backgroundColors; @property (strong, nonatomic) NSMutableDictionary *titleColors; @@ -53,7 +53,7 @@ - (NSDate *)dateForIndexPath:(NSIndexPath *)indexPath; - (NSIndexPath *)indexPathForDate:(NSDate *)date; -- (void)scrollToCurrentDate; +- (void)scrollToDate:(NSDate *)date; @end @@ -187,7 +187,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"contentSize"]) { - [self scrollToCurrentDate]; + [self scrollToDate:_currentDate]; [_collectionView removeObserver:self forKeyPath:@"contentSize"]; } } @@ -239,7 +239,7 @@ [_collectionView setContentOffset:destOffset animated:YES]; } [cell showAnimation]; - self.selectedDate = cell.date; + _selectedDate = cell.date; [self didSelectDate:cell.date]; } @@ -267,11 +267,6 @@ self.currentMonth = currentMonth; } -- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate -{ - -} - #pragma mark - Setter & Getter - (void)setFlow:(FSCalendarFlow)flow @@ -311,6 +306,15 @@ return (FSCalendarFlow)_collectionViewFlowLayout.scrollDirection; } +- (void)setSelectedDate:(NSDate *)selectedDate +{ + if (![_selectedDate isEqualToDate:selectedDate]) { + _selectedDate = [selectedDate copy]; + [self scrollToDate:_selectedDate]; + [_collectionView selectItemAtIndexPath:[self indexPathForDate:_selectedDate] animated:NO scrollPosition:UICollectionViewScrollPositionNone]; + } +} + - (void)setWeekdayFont:(UIFont *)weekdayFont { if (_weekdayFont != weekdayFont) { @@ -340,7 +344,7 @@ if (![_currentDate isEqualToDate:currentDate]) { _currentDate = [currentDate copy]; _currentMonth = [currentDate copy]; - [self scrollToCurrentDate]; + [self scrollToDate:_currentDate]; } } @@ -607,16 +611,16 @@ #pragma mark - Private -- (void)scrollToCurrentDate +- (void)scrollToDate:(NSDate *)date { - NSInteger scrollOffset = [_currentDate fs_monthsFrom:[NSDate dateWithTimeIntervalSince1970:0]]; - scrollOffset += _currentDate.fs_day == 1; + NSInteger scrollOffset = [date fs_monthsFrom:[NSDate dateWithTimeIntervalSince1970:0]]; + scrollOffset += date.fs_day == 1; if (self.flow == FSCalendarFlowHorizontal) { - _collectionView.bounds = CGRectOffset(_collectionView.bounds, + _collectionView.bounds = CGRectOffset(self.bounds, scrollOffset * _collectionView.fs_width, 0); } else if (self.flow == FSCalendarFlowVertical) { - _collectionView.bounds = CGRectOffset(_collectionView.bounds, + _collectionView.bounds = CGRectOffset(self.bounds, 0, scrollOffset * _collectionView.fs_height); }