• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - DSLCalendarView 的自定义实现,用于日历等 expedia

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:15:25 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我一直在尝试修改存储库, https://github.com/PeteC/DSLCalendarView允许用户通过点击并自动选择两者之间的日期来选择开始和结束日期。我通过在随附的演示中实现以下代码来实现这一点:

问题在于它破坏了拖动日历以选择日期范围的原始实现。

非常感谢任何帮助/指导,如果您知道任何其他实现相同目标的库,我将不胜感激。我正在寻找的功能是: 允许用户选择第一个日期、最后一个日期并在中间显示日期作为选择。

在 ViewController.m 中

- (DSLCalendarRange*)calendarViewDSLCalendarView *)calendarView didDragToDayNSDateComponents *)day selectingRangeDSLCalendarRange *)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)touchesEndedNSSet *)touches withEventUIEvent *)event {
//added condition here besides the other code
if ([self.delegate respondsToSelectorselector(hasSelectedStartDate)]) {
    self.flag = [self.delegate performSelectorselector(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)touchesBeganNSSet *)touches withEventUIEvent *)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 respondsToSelectorselector(calendarView:didDragToDay:selectingRange]) {
    newRange = [self.delegate calendarView:self didDragToDay:touchedView.day selectingRange:newRange];
    }
    self.selectedRange = newRange;

    [self positionCalloutViewForDayView:touchedView]; }

- (void)touchesMovedNSSet *)touches withEventUIEvent *)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 respondsToSelectorselector(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)touchesEndedNSSet *)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 respondsToSelectorselector(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 respondsToSelectorselector(calendarView:didSelectRange]) {
        [self.delegate calendarView:self didSelectRange:self.selectedRange];
    }
     }



Best Answer-推荐答案


您可以通过它自己的演示示例来实现这一点,

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

用这个替换它,

- (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]];
}

关于ios - DSLCalendarView 的自定义实现,用于日历等 expedia,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846379/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap