Merge pull request #53 from f33chobits/0.9.2

0.9.2
This commit is contained in:
Wenchao Ding 2015-07-19 21:52:21 +08:00
commit 581dfe6fa3
3 changed files with 111 additions and 30 deletions

View File

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

View File

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

View File

@ -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)
<br/>
![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)