在ios6中,将pageViewController的gestureRecognizers委托设置为viewController会导致崩溃 [英] In ios6, setting your pageViewController's gestureRecognizers delegate to a viewController causes a crash

查看:488
本文介绍了在ios6中,将pageViewController的gestureRecognizers委托设置为viewController会导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是在ios6上开始发生,但如果你使用页面视图控制器模板启动一个新项目。然后在

This has only started happening with ios6, but if you start a new project using the page view controller template. Then in

PCRootViewControlle::viewDidLoad()

将行添加到方法的底部。

add the lines to the bottom of the method.

for (UIGestureRecognizer *gR in self.pageViewController.gestureRecognizers)
{
    gR.delegate = self;
}

您需要分配viewController,使其符合UIGestureRecognizerDelegate并实施方法

You'll need to assign the viewController so it conforms to the UIGestureRecognizerDelegate and implement the method

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch (UITouch *)touch
{
    return YES;
}

现在,如果您运行应用程序并尝试将页面超出界限,即转到1月并尝试返回所以

Now if you run the app and try to turn the page beyond the bounds, i.e. go to January and try to turn back so


  • (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController

返回nil。

该应用程序将崩溃。

ios5没有发生这种情况。我需要将gestureRecognizer委托分配给我的viewController,因为我并不总是希望pageViewController处理触摸事件。

This did not happen with ios5. I need to assign the gestureRecognizer delegate to my viewController because I do not always want the pageViewController to handle the touch events.

有没有其他人经历过这个或指出如果我做错了什么?

Has any else experienced this or point out If I am doing something wrong?

非常感谢
斯图尔特。

Many Thanks Stewart.

推荐答案

终于找到了解决我问题的办法,这让我感到悲痛,所以希望这可以帮助别人。

Finally found a solution to my problem which has caused me grief so hopefully this can help someone else.

问题是如果你将pageViewControllers委托设置为你的viewController

The issue is if you set the pageViewControllers delegate to your viewController

for (UIGestureRecognizer *gR in self.pageController.view.gestureRecognizers) 
{
    if ([gR isKindOfClass:[UITapGestureRecognizer class]])
    {
        gR.enabled = NO;
    }
    else if ([gR isKindOfClass:[UIPanGestureRecognizer class]])
    {
        gR.delegate = self;
    }
}

然后从

pageViewController:viewControllerAfterViewController:

会崩溃!只在iOS6中!!

will crash!! only in iOS6!!

我的问题是我需要设置gestureRecognisers的委托,因为在某些情况下我需要截取panGesture,即不允许用户由于某些按钮在那里触摸某些部分,所以翻页。

My issue was that I needed to set the delegate of the gestureRecognisers because I needed to intercept the panGesture in some situations, i.e not allowing the user to turn a page wilst touching certain parts of it due to some buttons there.

解决方案是将逻辑从

pageViewController:viewControllerAfterViewController: 

into

gestureRecognizer:shouldReceiveTouch:

因为只要我们从那里返回NO那么它就不会继续调用

because as long as we return NO from that then it wont go on to call

pageViewController:viewControllerAfterViewController:

所以不需要返回nil并且崩溃。

and so no need to return nil and get a crash.

但是,这在序列的第一页和最后一页上不起作用。例如,在第一页上,您希望允许页面前进但不返回。所以我想看看传入的GestureRecogniser,将其投射到PanGesture,然后检查速度,如果速度表示转回(> 0.0f),则返回NO。这听起来不错,但速度始终为零。

However, this didn't work on the first and last page in the sequence. For example on the first page, you want to allow the page to turn forward but not back. So I thought about looking at the GestureRecogniser passed in, casting it to a PanGesture, and then checking the velocity on this, if the velocity signifies turning back ( > 0.0f ) then just return NO. This sounded good but the velocity was always zero.

然后我在GestureRecognizer委托上发现了一个非常有用的小函数:

I then found a very helpful little function on the GestureRecognizer delegate called:

gestureRecognizerShouldBegin:gestureRecognizer

此函数在

gestureRecognizer:shouldReceiveTouch:

但这一次手势的速度是我预期的,所以我可以检查速度,如果它> 0.0f

but this time the velocity from the gesture is as I expected, so I can check the velocity and only return YES for the first page if it is > 0.0f

这篇关于在ios6中,将pageViewController的gestureRecognizers委托设置为viewController会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆