Question

I need to perform a specific action when the user swipes the uicollectionview. I built it in a way that each cell captures the full screen.

I tried those ways:

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

But the scrollVelocity returns null. The method is being called.

B. UISwipeGestureRecognizer

In ViewDidLoad of my UIViewController which delegates to UICollectionViewDataSource and UIGestureRecognizerDelegate I added:

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

and the followings in the 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;
}

But none are called.

What is wrong? I am developing for ios7

Was it helpful?

Solution

You are not setting the delegate of the gestures:

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

OTHER TIPS

i was trying to do same thing with you (by full size cell frames in a UICollectionView). Just delegating with self is not sufficient enought, because UICollectionView may has it's own gesture recognizer. So below trick was worked in may case.

    override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = .red

    let leftGR = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeHandler_Left(gestureRecognizer:)))
    leftGR.numberOfTouchesRequired = 1
    leftGR.direction = .left
    leftGR.delegate = self

    self.view.addGestureRecognizer(leftGR)
}

@objc func swipeHandler_Left(gestureRecognizer : UISwipeGestureRecognizer!) {
    if gestureRecognizer.state == .ended {
        // Perform action.
        print("***Free to Delete Old Cell")
    }
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

sorry for my swifty answer, pls try to translate objc :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top