禁用UIPageViewController中的滑动

时间:2015-05-13 00:42:10

标签: ios swift uiviewcontroller uipageviewcontroller

我有UIViewControllerUIPageViewController变量。

我正在尝试禁用屏幕上某些位置的UIPageViewController滑动。我这样做是将UIViewController设置为UIGestureRecognizerDelegate,并将UIPageViewController委托中的手势识别器设置为self(UIViewController)。我似乎无法找到手势识别器的位置或为UIPageViewController加载。

我已经尝试了这三个地方,并且在viewDidLoad方法中都是空的:

println("gesture\(self.pageViewController!.gestureRecognizers)")
println("gesture\(self.view.gestureRecognizers)")
println("gesture\(self.pageViewController!.view.gestureRecognizers)")

UPDATE *******************:

所以我发现一些UIGestureRecognizers隐藏在navigationController中,但是将它们添加为委托并运行下面的代码什么也没做。是因为我有错误的UIGestureRecognizers。其中有一个是UIPanGestureRecognizer。为什么我的页面控制器或页面控制器的视图中没有任何识别器?

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    println("Should recieve touch")
    return false
}

我收到“应该接触”以打印到日志但我返回false并且没有禁用滑动。我做错了什么?

3 个答案:

答案 0 :(得分:2)

过去我曾试图用UIScrollView做类似的事情。 UIScrollView的成员名为panGesture。但是你可以像这样在pageViewControllerGesture上循环(在标准的基于页面的应用程序上测试):

 override func viewDidLoad() {
    super.viewDidLoad()

    // other set up

    // loop over your pageViewController gestures
    for gesture in self.pageViewController!.gestureRecognizers
    {
        // get the good one, i discover there are 2
        if(gesture is UIPanGestureRecognizer)
        {
            // replace delegate by yours (Do not forget to implement the gesture protocol)
            (gesture as! UIPanGestureRecognizer).delegate = self
        }
    }

}

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool
{
    // add custom logic
    var ShouldISwipe = rand()%2 == 0
    println("Should I Swipe : \(ShouldISwipe)")
    return ShouldISwipe
}

我发现链接也可以帮助(应该转换为swift): http://www.lukaszielinski.de/blog/posts/2014/03/26/restrict-panning-of-uipageviewcontroller-to-certain-area/

希望可以提供帮助。

答案 1 :(得分:1)

您可以使用以下命令获取UIPageViewController手势识别器:

UIPageViewController.view.subviews.first

在我的测试中,我得到:

    - [0] : <UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x17e57f20; state = Possible; delaysTouchesBegan = YES; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=delayed:, target=<_UIQueuingScrollView 0x1842b800>)>>
- [1] : <UIScrollViewPanGestureRecognizer: 0x17e58470; state = Possible; delaysTouchesBegan = YES; delaysTouchesEnded = NO; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=handlePan:, target=<_UIQueuingScrollView 0x1842b800>)>; must-fail = {
    <UIScrollViewPagingSwipeGestureRecognizer: 0x17d50110; state = Possible; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=_handleSwipe:, target=<_UIQueuingScrollView 0x1842b800>)>>
}>
- [2] : <UIScrollViewPagingSwipeGestureRecognizer: 0x17d50110; state = Possible; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=_handleSwipe:, target=<_UIQueuingScrollView 0x1842b800>)>; must-fail-for = {
    <UIScrollViewPanGestureRecognizer: 0x17e58470; state = Possible; delaysTouchesBegan = YES; delaysTouchesEnded = NO; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=handlePan:, target=<_UIQueuingScrollView 0x1842b800>)>>
}>

答案 2 :(得分:0)

您可以将此扩展名用于swift:

extension UIPageViewController {
    var isPagingEnabled: Bool {
        get {
            var isEnabled: Bool = true
            for view in view.subviews {
                if let subView = view as? UIScrollView {
                    isEnabled = subView.isScrollEnabled
                }
            }
            return isEnabled
        }
        set {
            for view in view.subviews {
                if let subView = view as? UIScrollView {
                    subView.isScrollEnabled = newValue
                }
            }
        }
    }
}

并称呼它:

pageCtrl.isPagingEnabled = false