7

Мне нужно выполнить определенное действие, когда пользователь просматривает uicollectionview. Я построил его так, чтобы каждая ячейка захватывала весь экран.Обнаружение прокрутки в UICollectionView

Я попробовал эти способы:

А. 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 возвращает нуль. Вызывается метод.

B. UISwipeGestureRecognizer

В ViewDidLoad моих UIViewController которые делегатов UICollectionViewDataSource и UIGestureRecognizerDelegate я добавил:

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]; 
Смежные вопросы