2

我一直在尝试修改存储库 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 中,我将以下代码添加到 touchEnded 以补充上述实现:

//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];
    }
     }
4

3 回答 3

1

您可以从它自己的演示示例中实现这一点,

只需在 ViewController.m 中寻找- (void)calendarView:(DSLCalendarView *)calendarView didSelectRange:(DSLCalendarRange *)range委托

用这个代替它,

- (void)calendarView:(DSLCalendarView *)calendarView didSelectRange:(DSLCalendarRange *)range
{
    if (range != nil)
    {
    if(startD == nil) {
        if(endD == nil) {
            [self start:nil];
        }else{
            [self end:nil];
        }
    }else{
        if(endD == nil) {
            [self end:nil];
        }
    }

    [self draw:nil];
}

现在找- (IBAction)draw:(id)sender同班,

用这个代替它,

- (IBAction)draw:(id)sender {
    if(startD && endD) {
        [_calendarView setSelectedRange:[[DSLCalendarRange alloc] initWithStartDay:startD.startDay endDay:endD.endDay]];
        startD = nil;
        endD = nil;
    }

    else if(startD)
    [_calendarView setSelectedRange:[[DSLCalendarRange alloc] initWithStartDay:startD.startDay endDay:startD.endDay]];
    else if(endD)
        [_calendarView setSelectedRange:[[DSLCalendarRange alloc] initWithStartDay:endD.startDay endDay:endD.endDay]];
}
于 2014-01-11T12:39:33.017 回答
0

CXDurationPicker 是我能找到的最接近 expedia 日历的组件。 https://github.com/concurlabs/CXDurationPicker

为了模仿 expedia 日历选择器的行为,我还做了一些更改。

  1. 定义自己的委托

    #pragma mark - CXDurationPickerViewDelegate
    - (void)durationPicker:(CXDurationPickerView *)durationPicker
            endDateChanged:(CXDurationPickerDate)pickerDate {
    
        NSDate *firstDate = [CXDurationPickerUtils dateFromPickerDate:durationPicker.startDate];
        NSDate *secondDate = [CXDurationPickerUtils dateFromPickerDate:durationPicker.endDate];
    
        [self updateStartDate:firstDate EndDate:secondDate AtIndex:selectedId];
        [self dismissDatePicker:nil];
    }
    
    - (void)durationPicker:(CXDurationPickerView *)durationPicker
         singleDateChanged:(CXDurationPickerDate)pickerDate {
        NSLog(@"singleDateChanged");
    }
    
    - (void)durationPicker:(CXDurationPickerView *)durationPicker
          startDateChanged:(CXDurationPickerDate)pickerDate {
    
        durationPicker.endDate = durationPicker.startDate;
        durationPicker.mode = CXDurationPickerModeEndDate;
    }
    
    #pragma mark - CXDurationPickerViewDelegate Optionals
    
    - (void)durationPicker:(CXDurationPickerView *)durationPicker
    invalidEndDateSelected:(CXDurationPickerDate)date {
    
        NSLog(@"Invalid end date selected.");
    
        durationPicker.startDate = durationPicker.endDate = date;
        durationPicker.mode = CXDurationPickerModeEndDate;
    }
    
    - (void)durationPicker:(CXDurationPickerView *)durationPicker invalidStartDateSelected:(CXDurationPickerDate)date {
        NSLog(@"Invalid start date selected.");
        durationPicker.startDate = durationPicker.endDate = date;
        durationPicker.mode = CXDurationPickerModeEndDate;
    }
    
    - (void)durationPicker:(CXDurationPickerView *)durationPicker
       didSelectDateInPast:(CXDurationPickerDate)date
                   forMode:(CXDurationPickerMode)mode {
    
        NSLog(@"Date was selected in the past. Ignoring.");
    }
    
  2. 更新 CXDurationPickerDayView.m 中的代码以修复开始日和结束日为同一天时的显示问题。

    --- a/Popsoc/CXDurationPicker/CXDurationPickerView.m
    +++ b/Popsoc/CXDurationPicker/CXDurationPickerView.m
    @@ -482,8 +482,12 @@
         day.type = CXDurationPickerDayTypeStart;
    
         day = (CXDurationPickerDayView *) [self.days lastObject];
    
         day.type = CXDurationPickerDayTypeEnd;
    +
    +    if (self.days.count == 1) {
    +        day.type = CXDurationPickerDayTypeSingle;
    +    }
    
于 2015-06-08T07:02:06.917 回答
0

我修改了 DSLCalendarView 库,它在两个方面都像魅力一样工作,1. 选择持续时间拖动 2. 类似 Expedia 的点击。

在 DSLCalendarView.m 文件中,找到 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 方法。在这个方法中,你会在下面找到 if 语句

if (!self.draggedOffStartDay && [self.draggingStartDay isEqual:touchedView.day]) {
    // comment out this original code
    // self.selectedRange = [[DSLCalendarRange alloc] initWithStartDay:touchedView.day endDay:touchedView.day];

    // add below code block
    if (!self.startDate) {
        self.startDate = self.draggingStartDay;
        self.selectedRange = [[DSLCalendarRange alloc] initWithStartDay:self.draggingStartDay
                                                                 endDay:touchedView.day];
    }
    else if (self.startDate && !self.endDate) {
        self.endDate = touchedView.day;
        self.selectedRange = [[DSLCalendarRange alloc] initWithStartDay:self.startDate
                                                                 endDay:touchedView.day];
    }
    else if (self.startDate && self.endDate) {
        self.startDate = self.draggingStartDay;
        self.endDate = nil;
        self.selectedRange = [[DSLCalendarRange alloc] initWithStartDay:self.draggingStartDay
                                                                 endDay:touchedView.day];
    }
}

不要忘记声明两个全局变量 @property (nonatomic, strong) NSDateComponents *startDate; @property (nonatomic, strong) NSDateComponents *endDate; 在同一个文件中。

您可以通过在 VC 中实现以下委托方法来检查结果。

#pragma mark - DSLCalendarViewDelegate methods

- (void)calendarView:(DSLCalendarView *)calendarView didSelectRange:(DSLCalendarRange *)range {
    if (range) {
        NSLog(@"selected %ld/%ld/%ld - %ld/%ld/%ld", range.startDay.year, range.startDay.month, range.startDay.day, range.endDay.year, range.endDay.month, range.endDay.day);
    }
}
于 2015-09-04T09:13:55.553 回答