Make selectedDate writable

Make selectedDate writable
This commit is contained in:
f33chobits 2015-03-16 20:56:57 +08:00
parent 1d8ecb49dc
commit 7cf8db383f
3 changed files with 25 additions and 16 deletions

View File

@ -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

View File

@ -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;

View File

@ -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);
}