Skip to content

Commit

Permalink
添加结束时间
Browse files Browse the repository at this point in the history
  • Loading branch information
Arc-lin committed Jun 19, 2017
1 parent f7046e9 commit a2bc6f7
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 13 deletions.
2 changes: 1 addition & 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.1.0"
s.version = "1.2.0"
s.summary = "简洁简单的日历选择器"

# This description is used to generate tags and improve search results.
Expand Down
3 changes: 3 additions & 0 deletions ALCalendarPicker/ALCalendarHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
/** 起始年月 yyyy-MM */
@property (nonatomic, copy) NSString *beginYearMonth;

/** 结束年月 yyyy-MM */
@property (nonatomic, copy) NSString *endYearMonth;

/** 代理 */
@property (nonatomic, assign) id<ALCalendarHeaderDelegate> delegate;

Expand Down
28 changes: 17 additions & 11 deletions ALCalendarPicker/ALCalendarHeader.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ @interface ALCalendarHeader()

@property (nonatomic, strong) UIButton *rightBtn;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *titleButton;

@end

Expand All @@ -37,7 +37,7 @@ - (instancetype)initWithFrame:(CGRect)frame
- (void)setupView
{
[self addSubview: self.leftBtn];
[self addSubview: self.titleLabel];
[self addSubview: self.titleButton];
[self addSubview: self.rightBtn];

__weak typeof(self) weakSelf = self;
Expand All @@ -50,7 +50,7 @@ - (void)setupView
make.width.equalTo(strongSelf.mas_height);
}];

[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
[self.titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
__strong typeof(weakSelf) strongSelf = weakSelf;
make.centerX.equalTo(strongSelf.mas_centerX);
make.centerY.equalTo(strongSelf.mas_centerY);
Expand Down Expand Up @@ -112,28 +112,34 @@ - (UIButton *)rightBtn
return _rightBtn;
}

- (UILabel *)titleLabel
- (UIButton *)titleButton
{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:16];
_titleLabel.textColor = [UIColor darkGrayColor];
if (!_titleButton) {
_titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
_titleButton.titleLabel.font = [UIFont systemFontOfSize:16];
[_titleButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
}
return _titleLabel;
return _titleButton;
}

- (void)setTitle:(NSString *)title
{
_title = title;
// 格式化显示日期
NSArray *dates = [title componentsSeparatedByString:@"-"];
self.titleLabel.text = [NSString stringWithFormat:@"%@年 - %@",dates.firstObject,dates.lastObject];
[self.titleButton setTitle:[NSString stringWithFormat:@"%@年 - %@",dates.firstObject,dates.lastObject] forState:UIControlStateNormal];

if([title isEqualToString:self.beginYearMonth]) {
if ([title isEqualToString:self.beginYearMonth]) {
self.leftBtn.enabled = NO;
} else {
self.leftBtn.enabled = YES;
}

if ([title isEqualToString:self.endYearMonth]) {
self.rightBtn.enabled = NO;
} else {
self.rightBtn.enabled = YES;
}
}

@end
2 changes: 1 addition & 1 deletion ALCalendarPicker/ALCalendarHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/** 根据当前年月得出日历数组 */
+ (NSArray *)datesWithYearAndMonth:(NSString *)ym;

/** 当前的年份和月份 yyyy-MM */
/** 今天的年份和月份 yyyy-MM */
+ (NSString *)currentYearAndMonth;

/** 今天 yyyy-MM-dd */
Expand Down
3 changes: 3 additions & 0 deletions ALCalendarPicker/ALCalendarPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
/** 起始年月 yyyy-MM */
@property (nonatomic, copy) NSString *beginYearMonth;

/** 结束年月 yyyy-MM */
@property (nonatomic, copy) NSString *endYearMonth;

@property (nonatomic, assign) id<ALCalendarPickerDelegate> delegate;

/** 高亮日期 yyyy-MM-dd 格式 */
Expand Down
34 changes: 34 additions & 0 deletions ALCalendarPicker/ALCalendarPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ - (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.endYearMonth] && scrollView.contentOffset.x > self.width) {
[scrollView setContentOffset:CGPointMake(self.width, 0) animated:NO];
}
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
Expand Down Expand Up @@ -196,6 +201,21 @@ - (void)refreshCollectionView:(BOOL)next
self.collectionViews[1].yearAndMonth = self.header.title;
}

// 比较时间先后
- (BOOL)compareOneDay:(NSString *)day1 withAnotherDay:(NSString *)day2
{
NSDate *dateA = [ALCalendarHelper dateStringToDate:day1 format:@"yyyy-MM"];
NSDate *dateB = [ALCalendarHelper dateStringToDate:day2 format:@"yyyy-MM"];
NSComparisonResult result = [dateA compare:dateB];
if (result == NSOrderedDescending) {
return NO; // Day1 在 Day2 之后
}
else if (result == NSOrderedAscending) {
return YES; // Day1 在 Day2 之前
}
return YES;
}

#pragma mark - setter & getter

- (ALCalendarHeader *)header
Expand All @@ -213,6 +233,20 @@ - (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],@"结束时间不能比开始时间早");
}
}

- (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],@"结束时间不能比开始时间早");
}
}

- (UIScrollView *)scrollView
Expand Down
1 change: 1 addition & 0 deletions ALCalendarPickerDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ - (void)viewDidLoad {
calP.delegate = self;
// 起始日期
calP.beginYearMonth = @"2017-01";
calP.endYearMonth = @"2017-11";
calP.hightLightItems = @[@"2017-06-17",@"2017-05-22",@"2017-06-12"];
calP.hightlightPriority = NO;

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

---

## [1.2.0](https://github.com/Arc-lin/ALCalendarPicker/releases/tag/1.2.0) (06/19/2017)

#### Update
* 添加了结束时间

## [1.1.1](https://github.com/Arc-lin/ALCalendarPicker/releases/tag/1.1.1) (06/19/2017)

#### Update
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
3. 点击日期代理回调
4. 支持设定起始日期

### 关于时间区间

目前日历的时间区间

`开始时间(如果有的话) - 今天年月 - 结束时间(如果有的话)`

之后的版本会有 另外的区间模式

`开始时间 - 结束时间`

### 使用

```
Expand Down

0 comments on commit a2bc6f7

Please sign in to comment.