ios - DSLCalendarView 的自定义实现,用于日历等 expedia
<p><p>我一直在尝试修改存储库,
<a href="https://github.com/PeteC/DSLCalendarView" rel="noreferrer noopener nofollow">https://github.com/PeteC/DSLCalendarView</a>允许用户通过点击并自动选择两者之间的日期来选择开始和结束日期。我通过在随附的演示中实现以下代码来实现这一点:</p>
<p><strong>问题在于它破坏了拖动日历以选择日期范围的原始实现。</strong></p>
<p>非常感谢任何帮助/指导,如果您知道任何其他实现相同目标的库,我将不胜感激。我正在寻找的功能是:
<strong>允许用户选择第一个日期、最后一个日期并在中间显示日期作为选择。</strong> </p>
<p>在 ViewController.m 中</p>
<pre><code>- (DSLCalendarRange*)calendarView:(DSLCalendarView *)calendarView didDragToDay:(NSDateComponents *)day selectingRange:(DSLCalendarRange *)range {
if (!self.startDate) {
// self.startDate = ];
//self.startDate = range.s
self.startDate = range.startDay;
self.hasSelectedStartDate = YES;
return [ initWithStartDay:self.startDate endDay:self.startDate];
NSLog(@"start date set to: %@",self.startDate);
}
else if (self.startDate && !self.endDate)
{
//self.endDate = ];
self.endDate = range.endDay;
NSLog(@"Start date is: %@",self.startDate);
NSLog(@"end date set to: %@",self.endDate);
return [ initWithStartDay:self.startDate endDay:self.endDate];
} else if (self.startDate && self.endDate)
{
return [ initWithStartDay:self.startDate endDay:self.endDate];
self.hasSelectedStartDate = NO;
}
NSLog(@"Select range programattically");
}
</code></pre>
<p>在 DSLCalendarView.m 中,我将以下代码添加到 touchEnded 以补充上述实现:</p>
<pre><code>//added code: Aakash
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//added condition here besides the other code
if () {
self.flag = ;
NSLog(@"Value: %hhd",self.flag);
}
if (!self.draggedOffStartDay && && !self.flag) {
self.selectedRange = [ initWithStartDay:touchedView.day endDay:touchedView.day];
}
</code></pre>
<p>触摸处理的原始代码是:</p>
<pre><code>#pragma mark - Touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
DSLCalendarDayView *touchedView = ;
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 = [ initWithStartDay:touchedView.day endDay:touchedView.day];
}
else if (! && !) {
newRange = [ initWithStartDay:touchedView.day endDay:touchedView.day];
}
else if () {
self.draggingFixedDay = self.selectedRange.endDay;
}
else {
self.draggingFixedDay = self.selectedRange.startDay;
}
if () {
newRange = ;
}
self.selectedRange = newRange;
; }
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.draggingStartDay == nil) {
return;
}
DSLCalendarDayView *touchedView = ;
if (touchedView == nil) {
self.draggingStartDay = nil;
return;
}
DSLCalendarRange *newRange;
if ( == NSOrderedAscending) {
newRange = [ initWithStartDay:touchedView.day endDay:self.draggingFixedDay];
}
else {
newRange = [ initWithStartDay:self.draggingFixedDay endDay:touchedView.day];
}
if () {
newRange = ;
}
self.selectedRange = newRange;
if (!self.draggedOffStartDay) {
if (!) {
self.draggedOffStartDay = YES;
}
}
; }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.draggingStartDay == nil) {
return;
}
DSLCalendarDayView *touchedView = ;
if (touchedView == nil) {
self.draggingStartDay = nil;
return;
}
if (!self.draggedOffStartDay && ) {
self.selectedRange = [ 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 () {
animateToAdjacentMonth = ];
}
if (animateToAdjacentMonth) {
if ( == NSOrderedAscending) {
;
}
else {
;
}
}
}
if () {
;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以通过它自己的演示示例来实现这一点,</p>
<p>只需在 ViewController.m 中查找 <code>- (void)calendarView:(DSLCalendarView *)calendarView didSelectRange:(DSLCalendarRange *)range</code> 委托(delegate)</p>
<p>用这个替换它,</p>
<pre><code>- (void)calendarView:(DSLCalendarView *)calendarView didSelectRange:(DSLCalendarRange *)range
{
if (range != nil)
{
if(startD == nil) {
if(endD == nil) {
;
}else{
;
}
}else{
if(endD == nil) {
;
}
}
;
}
</code></pre>
<p>现在在同一个类中寻找<code>- (IBAction)draw:(id)sender</code>,</p>
<p>用这个替换它,</p>
<pre><code>- (IBAction)draw:(id)sender {
if(startD && endD) {
initWithStartDay:startD.startDay endDay:endD.endDay]];
startD = nil;
endD = nil;
}
else if(startD)
initWithStartDay:startD.startDay endDay:startD.endDay]];
else if(endD)
initWithStartDay:endD.startDay endDay:endD.endDay]];
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - DSLCalendarView 的自定义实现,用于日历等 expedia,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19846379/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19846379/
</a>
</p>
页:
[1]