gpt4 book ai didi

ios - 自定义实现 DSLCalendarView for expedia like calendar

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:01 25 4
gpt4 key购买 nike

我一直在尝试修改存储库, 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];
}
}

最佳答案

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

只需在 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 for expedia like calendar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com