UICollectionView上的UISwipeGestureRecognizer不起作用

时间:2012-11-13 19:19:37

标签: objective-c ios

我尝试将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。

3 个答案:

答案 0 :(得分:3)

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

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

self.currentCollectionView.scrollEnabled = FALSE;

答案 1 :(得分:0)

尝试:

[self.currentCollectionView addGestureRecognizer:self.swipeDownRecognizer];

您目前还没有添加swipeUpRecognizer。

答案 2 :(得分:0)

错误的方法@aobs。对于希望滚动作为默认设置并根据滑动执行更多操作的人来说,它无效。在我的情况下,我不得不在手动滑动的情况下停止自动滚动行为,但在所有情况下都允许手动滚动。 (典型的促销行为)。

我认为

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

是没有委托

的代码中缺少的内容
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer     shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    NSLog(@"Asking permission");
    return YES;
}

没有在调试器中被击中。

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