Search code examples
iosswiftscrollviewswipe-gesture

Swipe Gesture Recognizer at ScrollView (Paging)


This is what I have on my Interface Builder using xcode 6.1.1

enter image description here

I have programmatically inserted 3 pages into ScrollView. I am able to swipe left and right to change page without issue.

        var view1 = UIView();
        view1.frame.size = CGSizeMake(self.view.frame.width, self.view.frame.height);
        view1.frame.origin = CGPointMake((self.view.frame.width * 0), 0);
        view1.backgroundColor = UIColor.redColor();

        var view2 = UIView();
        view2.frame.size = CGSizeMake(self.view.frame.width, self.view.frame.height);
        view2.frame.origin = CGPointMake((self.view.frame.width * 1), 0);
        view2.backgroundColor = UIColor.blueColor();

        var view3 = UIView();
        view3.frame.size = CGSizeMake(self.view.frame.width, self.view.frame.height);
        view3.frame.origin = CGPointMake((self.view.frame.width * 2), 0);
        view3.backgroundColor = UIColor.greenColor();

        scrollView.addSubview(view1);
        scrollView.addSubview(view2);
        scrollView.addSubview(view3);
        scrollView.contentSize = CGSizeMake(self.view.frame.width * 3, scrollView.frame.size.height);

What I need now is to detect swipe gesture on ScrollView. So far I have Swipe Gesture Recognizer works well on navigation bar and tab bar, but not the ScrollView.


Solution

  • Because UIScrollView has its own gesture recognizer you need to tell it to recognize another gesture recognizer:

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

    See the Controlling Simultaneous Gesture Recognition in the UIGestureRecognizerProtocol documentation.