リポジトリ https://github.com/PeteC/DSLCalendarViewを変更して、ユーザーが開始日と終了日をタップしてその間の日付を自動的に選択できるようにしようとしています。付随するデモに次のコードを実装することで、これを実現しました。
問題は、カレンダーをドラッグして日付範囲を選択する元の実装が壊れていることです。
ヘルプ/ガイダンスは大歓迎です。また、同じことを達成する他のライブラリを知っていれば、本当に感謝しています。私が探している機能は次のとおりです。 ユーザーが最初の日付、最後の日付を選択し、中央の日付を選択として表示できるようにします。
ViewController.m で
- (DSLCalendarRange*)calendarView:(DSLCalendarView *)calendarView didDragToDay:(NSDateComponents *)day selectingRange:(DSLCalendarRange *)range {
if (!self.startDate) {
// self.startDate = [self.dateFormatter dateFromString:[NSString stringWithFormat:@"%d/%d/%d",range.startDay.month, range.startDay.day,range.startDay.year]];
//self.startDate = range.s
self.startDate = range.startDay;
self.hasSelectedStartDate = YES;
return [[DSLCalendarRange alloc] initWithStartDay:self.startDate endDay:self.startDate];
NSLog(@"start date set to: %@",self.startDate);
}
else if (self.startDate && !self.endDate)
{
//self.endDate = [self.dateFormatter dateFromString:[NSString stringWithFormat:@"%d/%d/%d",range.startDay.month, range.startDay.day,range.startDay.year]];
self.endDate = range.endDay;
NSLog(@"Start date is: %@",self.startDate);
NSLog(@"end date set to: %@",self.endDate);
return [[DSLCalendarRange alloc] initWithStartDay:self.startDate endDay:self.endDate];
} else if (self.startDate && self.endDate)
{
return [[DSLCalendarRange alloc] initWithStartDay:self.startDate endDay:self.endDate];
self.hasSelectedStartDate = NO;
}
NSLog(@"Select range programattically");
}
DSLCalendarView.m では、上記の実装を補完するために、次のコードを touchedEnded に追加しました。
//added code: Aakash
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//added condition here besides the other code
if ([self.delegate respondsToSelector:@selector(hasSelectedStartDate)]) {
self.flag = [self.delegate performSelector:@selector(hasSelectedStartDate)];
NSLog(@"Value: %hhd",self.flag);
}
if (!self.draggedOffStartDay && [self.draggingStartDay isEqual:touchedView.day] && !self.flag) {
self.selectedRange = [[DSLCalendarRange alloc] initWithStartDay:touchedView.day endDay:touchedView.day];
}
タッチ処理の元のコードは次のとおりです。
#pragma mark - Touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
DSLCalendarDayView *touchedView = [self dayViewForTouches:touches];
if (touchedView == nil) {
self.draggingStartDay = nil;
return;
}
self.draggingStartDay = touchedView.day;
self.draggingFixedDay = touchedView.day;
self.draggedOffStartDay = NO;
DSLCalendarRange *newRange = self.selectedRange;
if (self.selectedRange == nil) {
newRange = [[DSLCalendarRange alloc] initWithStartDay:touchedView.day endDay:touchedView.day];
}
else if (![self.selectedRange.startDay isEqual:touchedView.day] && ![self.selectedRange.endDay isEqual:touchedView.day]) {
newRange = [[DSLCalendarRange alloc] initWithStartDay:touchedView.day endDay:touchedView.day];
}
else if ([self.selectedRange.startDay isEqual:touchedView.day]) {
self.draggingFixedDay = self.selectedRange.endDay;
}
else {
self.draggingFixedDay = self.selectedRange.startDay;
}
if ([self.delegate respondsToSelector:@selector(calendarView:didDragToDay:selectingRange:)]) {
newRange = [self.delegate calendarView:self didDragToDay:touchedView.day selectingRange:newRange];
}
self.selectedRange = newRange;
[self positionCalloutViewForDayView:touchedView]; }
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.draggingStartDay == nil) {
return;
}
DSLCalendarDayView *touchedView = [self dayViewForTouches:touches];
if (touchedView == nil) {
self.draggingStartDay = nil;
return;
}
DSLCalendarRange *newRange;
if ([touchedView.day.date compare:self.draggingFixedDay.date] == NSOrderedAscending) {
newRange = [[DSLCalendarRange alloc] initWithStartDay:touchedView.day endDay:self.draggingFixedDay];
}
else {
newRange = [[DSLCalendarRange alloc] initWithStartDay:self.draggingFixedDay endDay:touchedView.day];
}
if ([self.delegate respondsToSelector:@selector(calendarView:didDragToDay:selectingRange:)]) {
newRange = [self.delegate calendarView:self didDragToDay:touchedView.day selectingRange:newRange];
}
self.selectedRange = newRange;
if (!self.draggedOffStartDay) {
if (![self.draggingStartDay isEqual:touchedView.day]) {
self.draggedOffStartDay = YES;
}
}
[self positionCalloutViewForDayView:touchedView]; }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.draggingStartDay == nil) {
return;
}
DSLCalendarDayView *touchedView = [self dayViewForTouches:touches];
if (touchedView == nil) {
self.draggingStartDay = nil;
return;
}
if (!self.draggedOffStartDay && [self.draggingStartDay isEqual:touchedView.day]) {
self.selectedRange = [[DSLCalendarRange alloc] initWithStartDay:touchedView.day endDay:touchedView.day];
}
self.draggingStartDay = nil;
// Check if the user has dragged to a day in an adjacent month
if (touchedView.day.year != _visibleMonth.year || touchedView.day.month != _visibleMonth.month) {
// Ask the delegate if it's OK to animate to the adjacent month
BOOL animateToAdjacentMonth = YES;
if ([self.delegate respondsToSelector:@selector(calendarView:shouldAnimateDragToMonth:)]) {
animateToAdjacentMonth = [self.delegate calendarView:self shouldAnimateDragToMonth:[touchedView.dayAsDate dslCalendarView_monthWithCalendar:_visibleMonth.calendar]];
}
if (animateToAdjacentMonth) {
if ([touchedView.dayAsDate compare:_visibleMonth.date] == NSOrderedAscending) {
[self didTapMonthBack:nil];
}
else {
[self didTapMonthForward:nil];
}
}
}
if ([self.delegate respondsToSelector:@selector(calendarView:didSelectRange:)]) {
[self.delegate calendarView:self didSelectRange:self.selectedRange];
}
}