0

我尝试将 UISwipeGestureRecognizer 添加到我以编程方式创建的 UIColletionView 中,但识别器从未调用该操作。这是我的代码。

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.currentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake( 0.0f, 54.0f, 320.0f, 470.0f) collectionViewLayout:flowLayout];
[self.currentCollectionView setBackgroundColor:[UIColor whiteColor]];
self.currentCollectionView.delegate = self;
self.currentCollectionView.dataSource = self;
self.currentCollectionView.showsHorizontalScrollIndicator = NO;
self.currentCollectionView.showsVerticalScrollIndicator = NO;
self.currentCollectionView.scrollEnabled = YES;
self.currentCollectionView.bounces = YES;
[self.currentCollectionView setBackgroundColor:[UIColor lightGrayColor]];
[self.currentCollectionView registerClass:[TripexpPhotoCell class] forCellWithReuseIdentifier:@"photoCell"];
[self.view addSubview:self.currentCollectionView];
self.swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeUp:)];
self.swipeUpRecognizer.numberOfTouchesRequired = 1;
[self.swipeUpRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];

self.swipeDownRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeDown:)];
self.swipeDownRecognizer.numberOfTouchesRequired = 1;
[self.swipeDownRecognizer setDirection:UISwipeGestureRecognizerDirectionDown];

[self.currentCollectionView addGestureRecognizer:self.swipeDownRecognizer];
[self.currentCollectionView addGestureRecognizer:self.swipeUpRecognizer];

这是同时接收相同识别器的函数和委托

#pragma mark - UISwipeGestureRecognizer Action
-(void)didSwipeUp: (UISwipeGestureRecognizer*) recognizer {
    NSLog(@"Swiped Up");
}

-(void)didSwipeDown: (UISwipeGestureRecognizer*) recognizer {
    NSLog(@"Swiped Down");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer     shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    NSLog(@"Asking permission");
    return YES;
}

我还在 UICollectionView 内部循环并检查是否有任何现有的 UISwipeGestureRecognizer 但我没有找到。所以在我附加了我的 2 个识别器之后,我看到了那些 2。

4

3 回答 3

3

我知道这是一篇旧帖子,但这可能会对某人有所帮助,所以我在这里给出我的解决方案。

UICollectionView 继承自 UIScrollView。您需要先禁用滚动

self.currentCollectionView.scrollEnabled = FALSE;
于 2013-03-04T14:43:13.517 回答
0

尝试:

[self.currentCollectionView addGestureRecognizer:self.swipeDownRecognizer];

你也不是当前添加 swipeUpRecognizer。

于 2012-11-13T19:27:37.273 回答
0

错误的方法@aobs。它不适用于希望滚动作为默认值并根据滑动执行更多操作的人。在我的情况下,我必须在手动滑动的情况下停止自动滚动行为,但在所有情况下都允许手动滚动。(典型的促销报价行为)。

我认为

self.swipeDownRecognizer.delegate=self
self.swipeUpRecognizer.delegate=self

是代码中缺少的东西,因为没有委托

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer     shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    NSLog(@"Asking permission");
    return YES;
}

在调试器中没有受到影响。

我知道这是一个旧答案,但我希望它可以帮助搜索这个的人。

于 2016-07-20T14:10:07.600 回答