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返回null。該方法正在被調用。

B. UISwipeGestureRecognizer

在我UIViewController這代表ViewDidLoadUICollectionViewDataSourceUIGestureRecognizerDelegate我說:

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];