4

我已经为此花费了几个小时。我已经用 进行了初始化UIPageViewControllerUIPageViewControllerNavigationOrientationHorizontal但由于某种原因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;
    }
}

有任何想法吗?

4

1 回答 1

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

由于这个答案,我被暗示了这一点- 只要确保你UIPanGestureRecognizer只过滤掉 s 。

于 2012-09-19T13:14:50.660 回答