7

ユーザーがuicollectionviewをスワイプしたときに特定のアクションを実行する必要があります。 私は、各セルがフルスクリーンをキャプチャする方法で構築しました。UICollectionViewでスワイプを検出します。

A. scrollViewDidEndDecelerating

# pragma UIScrollView 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ 
    NSLog(@"detecting scroll"); 
    for (UICollectionViewCell *cell in [_servingTimesCollectionView visibleCells]) { 
     NSIndexPath *indexPath = [_servingTimesCollectionView indexPathForCell:cell]; 
     CGPoint scrollVelocity = [scrollView.panGestureRecognizer velocityInView:_servingTimesCollectionView]; 
     if (scrollVelocity.x > 0.0f) 
      NSLog(@"going right"); 
     else if (scrollVelocity.x < 0.0f) 
      NSLog(@"going left"); 
    } 
} 

しかしscrollVelocity戻りヌル:

は、私はそれらの方法を試してみました。メソッドが呼び出されています。私 UIViewControllerViewDidLoad

B. UISwipeGestureRecognizer

私は追加UICollectionViewDataSourceUIGestureRecognizerDelegateにその代表者:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)]; 
swipeRight.numberOfTouchesRequired = 1; 
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)]; 
swipeRight.numberOfTouchesRequired = 1; 
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 

[_servingTimesCollectionView addGestureRecognizer:swipeRight]; 
[_servingTimesCollectionView addGestureRecognizer:swipeLeft]; 

とのUIViewControllerで以下のことを:

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

-(void)didSwipeLeft: (UISwipeGestureRecognizer*) recognizer { 
    NSLog(@"Swiped Left"); 
} 

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

しかし、どれも呼び出されます。

どうしたのですか?私はあなたがジェスチャーのデリゲートを設定されていませんios7

答えて

8

のために開発しています:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)]; 
    swipeRight.delegate = self; 
    swipeRight.numberOfTouchesRequired = 1; 
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)]; 
    swipeLeft.delegate = self; 
    swipeLeft.numberOfTouchesRequired = 1; 
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
関連する問題