gpt4 book ai didi

ios - FSCalendar - 我想在 longPressGesture 上选择日期

转载 作者:行者123 更新时间:2023-11-28 23:47:54 25 4
gpt4 key购买 nike

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

我正在使用 WenchaoD/FSCalendar来自 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];
}

最佳答案

按住并多选:

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");
}

享受代码:)

关于ios - FSCalendar - 我想在 longPressGesture 上选择日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52368854/

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