UIPageViewController responds to vertical pan when orientation is set to horizontal

时间秒杀一切 提交于 2019-12-04 10:49:43
pxlshpr

I had initially stumbled across your question facing the same problem - but I seem to have fixed it for my situation.

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;

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

I was hinted towards this thanks to this answer - just make sure you filter out the UIPanGestureRecognizers only.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!