简体   繁体   中英

Swipe Gesture Recognizer at ScrollView (Paging)

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

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM