2012-11-13 18 views
0

我試圖將UISwipeGestureRecognizer添加到以編程方式創建的UIColletionView中,但識別器從不會調用該操作。這是我的代碼。UICollectionView上的UISwipeGestureRecognizer不起作用

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.

回答

0

嘗試:

[self.currentCollectionView addGestureRecognizer:self.swipeDownRecognizer]; 

也你是不是當前添加swipeUpRecognizer。

+0

它不工作,即使我加了自我。並且我還添加了swipeUpRecognizer –

3

我知道這是一箇舊帖子,但這可能有助於某人,所以我在這裏給我的解決方案。

UICollectionView繼承自UIScrollView。您需要先禁用滾動

self.currentCollectionView.scrollEnabled = FALSE; 
+2

爲什麼要禁用滾動?如果它被禁用 - 它不會滾動 – Dejell

+1

他正在使用滑動識別器。如果我們設置UICollectionView滾動,它與滑動衝突。我有同樣的問題,我通過禁用UICollectionView上的滾動來解決這個問題。此解決方案工作。如果UICollectionView滾動被禁用,則會調用滑動識別器。 – aobs

+0

不知道OP的問題,但解決了我的問題謝謝:) –

0

錯誤的方法@aobs。它不適用於那些希望將滾動設置爲默認設置的用戶,並根據滑動進行更多操作。在我的情況下,我不得不停止自動滾動行爲的情況下手動刷卡,但手動滾動將被允許在所有情況下。 (典型的促銷優惠行爲)。

我覺得

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

是因爲什麼在代碼中不缺少的委託

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

未在調試器擊中。

我知道這是一箇舊的答案,但我希望它可以幫助某人搜索這個。