Skip to content

Commit

Permalink
1.4.0更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Arclin committed Mar 28, 2018
1 parent 0c620e0 commit fb883d8
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 93 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion ALCalendarPicker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "ALCalendarPicker"
s.version = "1.3.0"
s.version = "1.4.0"
s.summary = "简洁简单的日历选择器"

# This description is used to generate tags and improve search results.
Expand All @@ -30,6 +30,7 @@ Pod::Spec.new do |s|
3. 点击日期代理回调
4. 支持设定起始日期
5. 支持点击给日期添加选择样式
6. 支持跳转到区间内某个年月
DESC

s.homepage = "https://github.com/Arc-lin/ALCalendarPicker"
Expand Down
8 changes: 4 additions & 4 deletions ALCalendarPicker/ALCalendarCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#import "ALCalendarHelper.h"
#import "ALCalendarConfig.h"

#import "UIView+Frame.h"
#import "UIView+ALFrame.h"

@interface ALCalendarCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

Expand Down Expand Up @@ -119,17 +119,17 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.width / 10, self.width / 10);
return CGSizeMake(self.al_width / 10, self.al_width / 10);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return ((self.width - 40) - (7 * self.width / 10)) / 7;
return ((self.al_width - 40) - (7 * self.al_width / 10)) / 7;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return ((self.width - 40) - (7 * self.width / 10)) / 7;
return ((self.al_width - 40) - (7 * self.al_width / 10)) / 7;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
Expand Down
2 changes: 1 addition & 1 deletion ALCalendarPicker/ALCalendarHeader.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#import "ALCalendarHelper.h"

#import "UIView+Frame.h"
#import "UIView+ALFrame.h"

#import <Masonry/Masonry.h>

Expand Down
3 changes: 3 additions & 0 deletions ALCalendarPicker/ALCalendarHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
/** 字符串转NSDate */
+ (NSDate *)dateStringToDate:(NSString *)dateString format:(NSString *)format;

/** NSDate转字符串 */
+ (NSString *)dateToDateString:(NSDate *)date format:(NSString *)format;

@end
23 changes: 23 additions & 0 deletions ALCalendarPicker/ALCalendarPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,28 @@

@protocol ALCalendarPickerDelegate <NSObject>

@optional

/**
选择某个日期
*/
- (void)calendarPicker:(ALCalendarPicker *)picker didSelectItem:(ALCalendarDate *)date date:(NSDate *)dateObj dateString:(NSString *)dateStr;

/**
点击按钮即将进入某个月历
@param yearMonth yyyy-MM格式
*/
- (void)calendarPicker:(ALCalendarPicker *)picker willClickToYearMonth:(NSString *)yearMonth;

/**
进入某个月历
@param yearMonth yyyy-MM格式
@param isScroll 判断是点击按钮进入日历或者滑动进入日历
*/
- (void)calendarPicker:(ALCalendarPicker *)picker didScrollToYearMonth:(NSString *)yearMonth isScroll:(BOOL)isScroll;

@end

@interface ALCalendarPicker : UIView
Expand Down Expand Up @@ -54,4 +71,10 @@
/** 选择日期的样式 */
- (void)setupSelectedItemStyle:(void(^)(UIColor **backgroundColor,NSNumber **backgroundCornerRadius,UIColor **titleColor))style;

/** 跳转到某个月历 只需提供日月 */
- (void)jumpToYearMonth:(NSDate *)date;

/** 字符串yyyy-MM 方式跳转 */
- (void)jumpToYearMonthByStr:(NSString *)dateStr;

@end
82 changes: 62 additions & 20 deletions ALCalendarPicker/ALCalendarPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#import "ALCalendarHelper.h"
#import "ALCalendarConfig.h"

#import "UIView+Frame.h"
#import "UIView+ALFrame.h"

#import <Masonry/Masonry.h>

Expand Down Expand Up @@ -69,7 +69,6 @@ - (void)setupView
make.bottom.equalTo(@0);
make.right.equalTo(@0);
}];

}

- (void)reloadPicker
Expand Down Expand Up @@ -136,6 +135,27 @@ - (void)setupSelectedItemStyle:(void(^)(UIColor **backgroundColor,NSNumber **bac
}
}

- (void)jumpToYearMonthByStr:(NSString *)dateStr
{
if (self.beginYearMonth && [self compareOneDay:dateStr withAnotherDay:self.beginYearMonth]) {
NSLog(@"❗️❗️跳转的时间不能在最早年月之前❗️❗️");
return;
} else if (self.endYearMonth && [self compareOneDay:self.endYearMonth withAnotherDay:dateStr]) {
NSLog(@"❗️❗️跳转的时间不能在最晚年月之后❗️❗️");
return;
}
self.header.title = dateStr;
self.collectionViews[1].yearAndMonth = dateStr;

[self setupLeftAndRightCalendar];
}

- (void)jumpToYearMonth:(NSDate *)date
{
NSString *dateStr = [ALCalendarHelper dateToDateString:date format:@"yyyy-MM"];
[self jumpToYearMonthByStr:dateStr];
}

#pragma mark - Private Method

- (void)showLeftCalendar
Expand All @@ -162,48 +182,64 @@ - (void)setupLeftAndRightCalendar
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 到达起始时间不能滚动
if ([self.header.title isEqualToString:self.beginYearMonth] && scrollView.contentOffset.x < self.width) {
[scrollView setContentOffset:CGPointMake(self.width, 0) animated:NO];
if ([self.header.title isEqualToString:self.beginYearMonth] && scrollView.contentOffset.x < self.al_width) {
[scrollView setContentOffset:CGPointMake(self.al_width, 0) animated:NO];
}

// 到达结束时间不能滚动
if ([self.header.title isEqualToString:self.endYearMonth] && scrollView.contentOffset.x > self.width) {
[scrollView setContentOffset:CGPointMake(self.width, 0) animated:NO];
if ([self.header.title isEqualToString:self.endYearMonth] && scrollView.contentOffset.x > self.al_width) {
[scrollView setContentOffset:CGPointMake(self.al_width, 0) animated:NO];
}
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self.scrollView setContentOffset:CGPointMake(self.width, 0) animated:NO];
[self.scrollView setContentOffset:CGPointMake(self.al_width, 0) animated:NO];

[self setupLeftAndRightCalendar];

if ([self.delegate respondsToSelector:@selector(calendarPicker:didScrollToYearMonth:isScroll:)]) {
[self.delegate calendarPicker:self didScrollToYearMonth:[self.header title] isScroll:NO];
}
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat offsetX = scrollView.contentOffset.x;
CGFloat page = offsetX / self.width;
CGFloat page = offsetX / self.al_width;
if (page == 2) {
[self showRightCalendar];
} else if (page == 0) {
[self showLeftCalendar];
}

[self.scrollView setContentOffset:CGPointMake(self.width, 0) animated:NO];
[self.scrollView setContentOffset:CGPointMake(self.al_width, 0) animated:NO];

// 配置上个月和下个月的数据源
[self setupLeftAndRightCalendar];

if ([self.delegate respondsToSelector:@selector(calendarPicker:didScrollToYearMonth:isScroll:)]) {
[self.delegate calendarPicker:self didScrollToYearMonth:[self.header title] isScroll:YES];
}
}

#pragma mark - ALCalendarHeaderDelegate

- (void)header:(ALCalendarHeader *)header didClickLeftBtn:(UIButton *)button
{
if ([self.delegate respondsToSelector:@selector(calendarPicker:willClickToYearMonth:)]) {
[self.delegate calendarPicker:self willClickToYearMonth:[ALCalendarHelper lastYearAndMonth:[self.header title]]];
}

[self showLeftCalendar];
}

- (void)header:(ALCalendarHeader *)header didClickRightBtn:(UIButton *)button
{
if ([self.delegate respondsToSelector:@selector(calendarPicker:willClickToYearMonth:)]) {
[self.delegate calendarPicker:self willClickToYearMonth:[ALCalendarHelper nextYearAndMonth:[self.header title]]];
}

[self showRightCalendar];
}

Expand All @@ -221,7 +257,7 @@ - (void)calendarView:(ALCalendarCollectionView *)calendarCollectionView didSelec
- (void)refreshCollectionView:(BOOL)next
{
if (next) { // 滚到下一页
[self.scrollView setContentOffset:CGPointMake(2 * self.width , 0) animated:YES];
[self.scrollView setContentOffset:CGPointMake(2 * self.al_width , 0) animated:YES];
} else { // 滚到上一页
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
Expand Down Expand Up @@ -261,19 +297,25 @@ - (void)setBeginYearMonth:(NSString *)beginYearMonth
{
_beginYearMonth = beginYearMonth;
self.header.beginYearMonth = beginYearMonth;
NSAssert([self compareOneDay:beginYearMonth withAnotherDay:[ALCalendarHelper currentYearAndMonth]], @"开始时间不能比当前月晚");
if (_endYearMonth) {
NSAssert([self compareOneDay:beginYearMonth withAnotherDay:self.endYearMonth],@"结束时间不能比开始时间早");

if (![self compareOneDay:beginYearMonth withAnotherDay:[ALCalendarHelper currentYearAndMonth]]) {
NSLog(@"❗️❗️开始时间不能比当前年月晚❗️❗️");
}
if (_endYearMonth && ![self compareOneDay:beginYearMonth withAnotherDay:self.endYearMonth]) {
NSLog(@"❗️❗️结束时间不能比开始时间早❗️❗️");
}
}

- (void)setEndYearMonth:(NSString *)endYearMonth
{
_endYearMonth = endYearMonth;
self.header.endYearMonth = endYearMonth;
NSAssert([self compareOneDay:[ALCalendarHelper currentYearAndMonth] withAnotherDay:endYearMonth], @"结束时间不能比当前月早");
if (_beginYearMonth) {
NSAssert([self compareOneDay:self.beginYearMonth withAnotherDay:endYearMonth],@"结束时间不能比开始时间早");

if ([self compareOneDay:endYearMonth withAnotherDay:[ALCalendarHelper currentYearAndMonth]]) {
NSLog(@"❗️❗️结束时间不能比当前月早❗️❗️");
}
if (_beginYearMonth && ![self compareOneDay:_beginYearMonth withAnotherDay:endYearMonth]) {
NSLog(@"❗️❗️结束时间不能比开始时间早❗️❗️");
}
}

Expand All @@ -282,11 +324,11 @@ - (UIScrollView *)scrollView
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] init];
_scrollView.pagingEnabled = YES;
CGFloat height = self.height - headerHeight;
_scrollView.contentSize = (CGSize){self.width * 3,height};
CGFloat height = self.al_height - headerHeight;
_scrollView.contentSize = (CGSize){self.al_width * 3,height};
_scrollView.delegate = self;
for (NSInteger i = 0; i < 3 ; i++) {
ALCalendarCollectionView *collectionView = [[ALCalendarCollectionView alloc] initWithFrame:CGRectMake(self.width * i, 0, self.width,height) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
ALCalendarCollectionView *collectionView = [[ALCalendarCollectionView alloc] initWithFrame:CGRectMake(self.al_width * i, 0, self.al_width,height) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
collectionView.collectionViewDelegate = self;
collectionView.yearAndMonth = nil;
if (i == 0) {
Expand All @@ -301,7 +343,7 @@ - (UIScrollView *)scrollView
}
_scrollView.showsHorizontalScrollIndicator = NO;
// _scrollView.scrollEnabled = NO;
[_scrollView setContentOffset:CGPointMake(self.width, 0) animated:NO];
[_scrollView setContentOffset:CGPointMake(self.al_width, 0) animated:NO];
}
return _scrollView;
}
Expand Down
24 changes: 24 additions & 0 deletions ALCalendarPicker/UIView+ALFrame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// UIView+Frame.h
// ArcBlog
//
// Created by Arclin on 15/11/18.
// Copyright © 2015年 sziit. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIView (ALFrame)

// 分类不能添加成员属性
// @property如果在分类里面,只会自动生成get,set方法的声明,不会生成带有下划线的成员属性,和方法的实现

@property (nonatomic,assign) CGFloat al_centerX;
@property (nonatomic,assign) CGFloat al_centerY;
@property (nonatomic,assign) CGSize al_size;
@property (nonatomic,assign) CGFloat al_x;
@property (nonatomic,assign) CGFloat al_y;
@property (nonatomic,assign) CGFloat al_width;
@property (nonatomic,assign) CGFloat al_height;

@end
Loading

0 comments on commit fb883d8

Please sign in to comment.