How to select date using LongpressGesture using FSCalendar??
I am using WenchaoD/FSCalendar third party calendar from github.
- (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];
}
Hold and multiple selection:
yourCalendarObject.swipeToChooseGesture.enabled = YES;
yourCalendarObject.allowsMultipleSelection = YES;
Hold and single selection:
[yourCalendarObject setAllowsSelection: YES];
[yourCalendarObject setAllowsMultipleSelection: NO];
yourCalendarObject.swipeToChooseGesture.enabled = YES;
EDIT: to difference the tap or hold action FSCalendar doesn't have that functionality but you can add with a single property.
FSCalendar.h
You need to add this property in your header file inside of:
@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
You need to add a set value of property in - (void)handleSwipeToChoose:(UILongPressGestureRecognizer *)pressGesture like this:
- (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;
}
}
Well you need to modify the method - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath like this:
- (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;
}
So you can try this:
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
if (calendar.isLongPressGesture)
NSLog(@"Long Press");
else
NSLog(@"Tap Gesture");
}
Enjoy the code :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.