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

时间:2015-03-18 07:46:03

标签: ios swift scrollview swipe-gesture

这是我使用xcode 6.1.1

在Interface Builder上的内容

enter image description here

我已经以编程方式将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。

1 个答案:

答案 0 :(得分:0)

由于UIScrollView有自己的手势识别器,您需要告诉它识别另一个手势识别器:

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

参见Controlling Simultaneous Gesture RecognitionUIGestureRecognizerProtocol文档中。