当方向设置为水平时,UIPageViewController响应垂直平移

时间:2012-06-29 06:03:15

标签: uipageviewcontroller

我已经烧了几个小时。我已使用UIPageViewController初始化UIPageViewControllerNavigationOrientationHorizontal,但出于某种原因,当用户垂直平移时会调用viewControllerBeforeViewController

此外,当发生这种情况时,没有页面翻转,并且根本没有调用didFinishAnimating:didFinishAnimating:previousViewControllers:transitionCompleted。这意味着页面视图控制器知道这是一个垂直移动..

这是初始化代码 -

- (void)initPageViewController
{
    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
                                                              navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal 
                                                                            options:nil];
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;

    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];

    // Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
    self.pageViewController.view.frame = self.view.bounds;

    [self.pageViewController didMoveToParentViewController:self];

    // Leave only pan recognizers enabled, so buttons positioned inside a page would work.
    for (UIGestureRecognizer *gr in self.pageViewController.gestureRecognizers)
    {
        if ([gr class] != [UIPanGestureRecognizer class])
            gr.enabled = NO;
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:7)

我最初偶然发现了遇到同样问题的问题 - 但我似乎已根据自己的情况修复了问题。

基本上,我要做的就是为所有附加到UIPageViewController的手势识别器设置一个委托。因此,在创建UIPageViewController后不久,我做了:

for (UIGestureRecognizer *gr in self.book.gestureRecognizers)
    gr.delegate = self.book;

其中book是我的自定义UIPageViewController(我基本上将自己设置为委托)。最后,将此方法添加到UIPageViewController以限制任何垂直平移(或使用注释掉的行来限制水平平移)。

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *panGestureRecognizer = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint translation = [panGestureRecognizer translationInView:self.view];

        return fabs(translation.x) > fabs(translation.y);
//      return fabs(translation.y) > fabs(translation.x);
    }
    else
        return YES;
}

感谢this answer,我向此暗示了这一点 - 请确保仅过滤掉UIPanGestureRecognizer