在 ScrollView(分页)上滑动手势识别器

Swipe Gesture Recognizer at ScrollView (Paging)

这是我使用 xcode 6.1.1

在我的 Interface Builder 上得到的

我已经以编程方式将 3 个页面插入到 ScrollView 中。我可以左右滑动切换页面,没有问题。

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

我现在需要的是在ScrollView 上检测滑动手势。到目前为止,我的 Swipe Gesture Recognizer 在导航栏和标签栏上运行良好,但在 ScrollView 上运行良好。

因为UIScrollView有自己的手势识别器,你需要告诉它识别另一个手势识别器:

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

查看 Controlling Simultaneous Gesture RecognitionUIGestureRecognizerProtocol 文档中。