7

Ich muss eine bestimmte Aktion ausführen, wenn der Benutzer die uicollectionview wischt. Ich habe es so gebaut, dass jede Zelle den ganzen Bildschirm einnimmt.Wischen in UICollectionView erkennen

habe ich versucht, diese Möglichkeiten:

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"); 
    } 
} 

Aber die scrollVelocity kehrt null. Die Methode wird aufgerufen.

B. UISwipeGestureRecognizer

In ViewDidLoad meiner UIViewController welche Delegierten UICollectionViewDataSource und UIGestureRecognizerDelegate ich hinzugefügt:

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

und das Folgende in der 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; 
} 

Aber keiner werden genannt.

Was ist los? Ich entwickle für ios7

Antwort

8

Sie setzen nicht die Delegierten der Gesten:

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