UIPageViewController在按下按钮时会崩溃

时间:2014-06-26 16:40:42

标签: ios iphone objective-c xcode uipageviewcontroller

我有UIPageViewController以编程方式设置动画。问题是它里面的视图控制器里面有UIButtons。当我按住一个按钮并等到UIPageViewController动画时,应用程序崩溃并出现错误:

'Failed to determine navigation direction for scroll'

我认为我需要做的是以某种方式伪造用户在UIPageviewController动画之前释放按钮。

但是,[self.button sendActionsForControlEvents:UIControlEventTouchCancel];似乎没有办法解决问题。也没有UIControlEventTouchUpInside

有没有更好的方法,或者我使用sendActionsForControlEvents错误?

1 个答案:

答案 0 :(得分:4)

所有sendActionsForControlEvents:都会调用您分配给按钮传入的控件事件的任何方法。它没有调用任何内部方法来以编程方式提升触摸或类似的东西。

在您以编程方式为页面视图控制器设置动画之前,请尝试使用此方法有效取消页面视图控制器内部滚动视图的平移手势识别器上的任何触摸:

- (void)cancelPanGestureTouchesOfPageViewController:(UIPageViewController *)pageVC
{
    // Since UIPageViewController doesn't provide any API to access its scroll view,
    // we have to find it ourselves by manually looping through its view's subviews.
    for (UIScrollView *scrollView in pageVC.view.subviews) {
        if ([scrollView isKindOfClass:[UIScrollView class]]) {

            // We've found the scroll view, so use this little trick to
            // effectively cancel any touches on its pan gesture recognizer
            BOOL enabled = scrollView.panGestureRecognizer.enabled;
            scrollView.panGestureRecognizer.enabled = !enabled;
            scrollView.panGestureRecognizer.enabled = enabled;
        }
    }
}

(请注意,这会弄乱UIPageViewController的内部视图层次结构,所以这种方法有点难看,将来可能会破坏。我一般不建议做这样的事情,但我想在这个例子应该没​​问题。)