如何使用FSCalendar使用LongpressGesture选择日期?
我正在使用来自github的WenchaoD/FSCalendar第三方日历。
- (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];
}
最佳答案
按住和多项选择:
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");
}
享受代码:)