FSCalendar-我想在longPressGesture上选择日期

时间:2018-09-17 13:26:18

标签: ios objective-c iphone github calendar

如何使用FSCalendar使用LongpressGesture选择日期?

我正在使用来自github的WenchaoD/FSCalendar第三方日历。

$html_table = '';

$html_table .= '<table>';
$html_table .= '<thead>';
$html_table .= '<th>Title</th>';
$html_table .= '<th>ISBN</th>';
$html_table .= '<th>Author</th>';
$html_table .= '</thead>';
$html_table .= '<tbody>';

foreach ($dataReturned as $line){
    $html_table .= '<tr>';
    $html_table .= "<th>$line[0]</th>";
    $html_table .= '<th>$line[1]</th>';
    $html_table .= '<th>$line[2]</th>';
    $html_table .= '</tr>';
}

$html_table .= '</tbody>';
$html_table .= '</table>';

echo $html_table;

1 个答案:

答案 0 :(得分:1)

按住并选择多项:

yourCalendarObject.swipeToChooseGesture.enabled = YES;
yourCalendarObject.allowsMultipleSelection = YES;

保留和单选:

[yourCalendarObject setAllowsSelection: YES];
[yourCalendarObject setAllowsMultipleSelection: NO];
yourCalendarObject.swipeToChooseGesture.enabled = YES;

编辑:要改变点击或保持动作,FSCalendar不具备该功能,但是您可以添加一个属性。

FSCalendar.h

您需要在以下文件的头文件中添加此属性:

@interface FSCalendar : UIView

/**
Difference the tap or hold gesture date selection
When the value = YES is a UILongPressGestureRecognizer
When the value = NO is a tap selection
*/

@property (readonly, nonatomic) BOOL isLongPressGesture;

@end

FSCalendar.m

您需要在-(void)handleSwipeToChoose:(UILongPressGestureRecognizer *)pressGesture中添加属性的设置值,如下所示:

- (void)handleSwipeToChoose:(UILongPressGestureRecognizer *)pressGesture
{
    switch (pressGesture.state) {
        case UIGestureRecognizerStateBegan:
        case UIGestureRecognizerStateChanged: {

            // New property value when the selection is UILongPressGestureRecognizer
            _isLongPressGesture = YES; 

            NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[pressGesture locationInView:self.collectionView]];
            if (indexPath && ![indexPath isEqual:self.lastPressedIndexPath]) {
                NSDate *date = [self.calculator dateForIndexPath:indexPath];
                FSCalendarMonthPosition monthPosition = [self.calculator monthPositionForIndexPath:indexPath];
                if (![self.selectedDates containsObject:date] && [self collectionView:self.collectionView shouldSelectItemAtIndexPath:indexPath]) {
                    [self selectDate:date scrollToDate:NO atMonthPosition:monthPosition];
                    [self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
                } else if (self.collectionView.allowsMultipleSelection && [self collectionView:self.collectionView shouldDeselectItemAtIndexPath:indexPath]) {
                    [self deselectDate:date];
                    [self collectionView:self.collectionView didDeselectItemAtIndexPath:indexPath];
                }
            }
            self.lastPressedIndexPath = indexPath;
            break;
        }
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled: {
            self.lastPressedIndexPath = nil;
            break;
        }
        default:
            break;
    }

}

那么您需要修改方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath ,像这样:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDate *selectedDate = [self.calculator dateForIndexPath:indexPath];
    FSCalendarMonthPosition monthPosition = [self.calculator monthPositionForIndexPath:indexPath];
    FSCalendarCell *cell;
    if (monthPosition == FSCalendarMonthPositionCurrent) {
        cell = (FSCalendarCell *)[collectionView cellForItemAtIndexPath:indexPath];
    } else {
        cell = [self cellForDate:selectedDate atMonthPosition:FSCalendarMonthPositionCurrent];
        NSIndexPath *indexPath = [collectionView indexPathForCell:cell];
        if (indexPath) {
            [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        }
    }
    if (![_selectedDates containsObject:selectedDate]) {
        cell.selected = YES;
        [cell performSelecting];
    }
    [self enqueueSelectedDate:selectedDate];
    [self.delegateProxy calendar:self didSelectDate:selectedDate atMonthPosition:monthPosition];
    [self selectCounterpartDate:selectedDate];

    // Reset the value of property
    _isLongPressGesture = NO;
}

因此您可以尝试以下操作:

- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    if (calendar.isLongPressGesture)
        NSLog(@"Long Press");
    else
        NSLog(@"Tap Gesture");
}

享受代码:)