问题描述
我为此花费了数小时.我已经用 UIPageViewControllerNavigationOrientationHorizontal
初始化了 UIPageViewController
,但是由于某种原因 viewControllerBeforeViewController
在用户垂直平移时被调用.
I've burnt hours on this. I've initialized UIPageViewController
with UIPageViewControllerNavigationOrientationHorizontal
, but for some reason viewControllerBeforeViewController
is called when user pans vertically.
此外,当发生这种情况时,不会发生翻页,并且根本不会调用 didFinishAnimating:didFinishAnimating:previousViewControllers:transitionCompleted
.这意味着页面视图控制器知道这是一个垂直移动..
Also, when this happens, there's no page flipping and didFinishAnimating:didFinishAnimating:previousViewControllers:transitionCompleted
isn't called at all. This means that the page view controller knows this is a vertical movement..
这是初始化代码-
- (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;
}
}
有什么想法吗?
推荐答案
我最初偶然发现您的问题面临同样的问题 - 但我似乎已经针对我的情况修复了它.
I had initially stumbled across your question facing the same problem - but I seem to have fixed it for my situation.
基本上,我必须为所有附加到 UIPageViewController
的手势识别器设置一个委托.所以,在创建 UIPageViewController
后不久,我做了:
Basically, what I had to do was set a delegate to all the gesture recognizers attached to the UIPageViewController
. So, soon after creating the UIPageViewController
, I did:
for (UIGestureRecognizer *gr in self.book.gestureRecognizers)
gr.delegate = self.book;
其中 book
是我的自定义 UIPageViewController
(我基本上将自己设置为委托).最后,将此方法添加到您的 UIPageViewController
以限制任何垂直平移(或使用注释掉的行来限制水平平移).
where book
is my custom UIPageViewController
(I had essentially set itself as the delegate). Finally, add this method to your UIPageViewController
to restrict any vertical panning (or use the commented out line instead to restrict horizontal panning).
- (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 only.
I was hinted towards this thanks to this answer - just make sure you filter out the UIPanGestureRecognizer
s only.
这篇关于当方向设置为水平时 UIPageViewController 响应垂直平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!