8

我对 UIPageViewController 有一个非常有趣的问题。

我的项目的设置与示例基于页面的应用程序模板非常相似。时不时(但在一定程度上可重现)某个平移手势会调用-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController.

我返回下一页的视图控制器,但从未运行过翻页动画,也从未调用过我的委托方法。

这是代码viewControllerAfterViewController

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    PageDisplayViewController *vc = (PageDisplayViewController *)viewController;
    NSUInteger index = [self.pageFetchController.fetchedObjects indexOfObject:vc.page];
    if(index == (self.pageFetchController.fetchedObjects.count - 1)) return nil;
    return [self getViewControllerForIndex:(++index)];
}

这里是getViewControllerForIndex:

-(PageDisplayViewController *)getViewControllerForIndex:(NSUInteger)index
{
    PageDisplayViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PageDisplayController"];
    newVC.page = [self.pageFetchController.fetchedObjects objectAtIndex:(index)];
    newVC.view.frame = CGRectMake(0, 0, 1024, 604);
    NSLog(@"%i", index);
    if(index == 0)
    {
        //We're moving to the first, animate the back button to be hidden.
        [UIView animateWithDuration:0.5 animations:^
        {
            self.backButton.alpha = 0.f;
        } completion:^(BOOL finished){
            self.backButton.hidden = YES;
        }];
    }
    else if(index == (self.pageFetchController.fetchedObjects.count - 1))
    {
        [UIView animateWithDuration:0.5 animations:^{
            self.nextButton.alpha = 0.f;
        } completion:^(BOOL finished){
            self.nextButton.hidden = YES;
        }];
    }
    else
    {
        BOOL eitherIsHidden = self.nextButton.hidden || self.backButton.hidden;
        if(eitherIsHidden)
        {
            [UIView animateWithDuration:0.5 animations:^{
                if(self.nextButton.hidden)
                {
                    self.nextButton.hidden = NO;
                    self.nextButton.alpha = 1.f;
                }
                if(self.backButton.hidden)
                {
                    self.backButton.hidden = NO;
                    self.backButton.alpha = 1.f;
                }
            }];
        }
    }
    return newVC;
}

基本上,我创建视图控制器,设置它的数据对象,然后根据索引淡出下一步/后退按钮。

委托方法

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    PageDisplayViewController *vc = [previousViewControllers lastObject];
    NSUInteger index = [self.pageFetchController.fetchedObjects indexOfObject:vc.page];

    if (!completed)
    {
        [self.pagePreviewView setCurrentIndex:index];
        NSLog(@"Animation Did not complete, reverting pagepreview");
    }
    else
    {
        PageDisplayViewController *curr = [pageViewController.viewControllers lastObject];
        NSUInteger i = [self.pageFetchController.fetchedObjects indexOfObject:curr.page];
        [self.pagePreviewView setCurrentIndex:i];
        NSLog(@"Animation compeleted, updating pagepreview. Index: %u", i);
    }
}

我只注意到这个问题,因为随机地,我的后退按钮会重新出现在屏幕上。在那里扔了一些NSLog()语句之后,我注意到我的 dataSource 方法被调用,索引为 1,但没有动画播放或委托被调用。更可怕的是,如果我尝试平移下一页,索引 1 会再次调用。

我担心这可能是 UIPageViewController 的错误。

4

4 回答 4

8

由于我在第一个答案中的实现仍然收到神秘的崩溃,我一直在寻找一个“足够好”的解决方案,它较少依赖于个人对页面视图控制器 (PVC) 底层行为的假设。这是我设法想出的。

我以前的方法有点侵入性,与其说是可接受的解决方案,不如说是一种解决方法。与其与 PVC 抗争迫使它做我认为应该做的事情,不如接受以下事实:

  • 和方法可以被 UIKit 调用任意次数,pageViewController:viewControllerBeforeViewController:并且pageViewController:viewControllerAfterViewController:
  • 绝对不能保证其中任何一个都对应于分页动画,也不能保证它们之后会调用pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:

这意味着我们不能将 before/after 方法用作“animation-begin”(但是请注意,它didFinishAnimating仍然用作“animation-end”事件)。那么我们如何知道动画确实已经开始了呢?

根据我们的需要,我们可能对以下事件感兴趣:

  1. 用户开始摆弄页面:一个很好的指标是之前/之后的回调,或者更准确地说是它们中的第一个。

  2. 翻页手势的第一个视觉反馈:我们可以state在 PVC 的点击和平移手势识别器的属性上使用 KVO。当UIGestureRecognizerStateBegan观察到一个平移值时,我们可以很确定视觉反馈会随之而来。

  3. 用户通过释放触摸完成拖动页面:再次,KVO。当该UIGestureRecognizerStateRecognized值被报告为平移或点击时,这是PVC实际要翻页的时候,所以这可以用作“动画开始”。

  4. UIKit 开始分页动画:我不知道如何获得直接反馈。

  5. UIKit 结束分页动画:小菜一碟,听听就好pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:

对于 KVO,只需获取 PVC 的手势识别器,如下所示:

@interface MyClass () <UIGestureRecognizerDelegate>
{
    UIPanGestureRecognizer* pvcPanGestureRecognizer;
    UITapGestureRecognizer* pvcTapGestureRecognizer;
}
...
for ( UIGestureRecognizer* recognizer in pageViewController.gestureRecognizers )
{
    if ( [recognizer isKindOfClass:[UIPanGestureRecognizer class]] )
    {
        pvcPanGestureRecognizer = (UIPanGestureRecognizer*)recognizer;
    }
    else if ( [recognizer isKindOfClass:[UITapGestureRecognizer class]] )
    {
        pvcTapGestureRecognizer = (UITapGestureRecognizer*)recognizer;
    }
}

然后将您的班级注册为该state属性的观察者:

[pvcPanGestureRecognizer addObserver:self
                          forKeyPath:@"state"
                             options:NSKeyValueObservingOptionNew
                             context:NULL];
[pvcTapGestureRecognizer addObserver:self
                          forKeyPath:@"state"
                             options:NSKeyValueObservingOptionNew
                             context:NULL];

并实现通常的回调:

- (void)observeValueForKeyPath:(NSString *)keyPath
        ofObject:(id)object
        change:(NSDictionary *)change
        context:(void *)context
{
    if ( [keyPath isEqualToString:@"state"] && (object == pvcPanGestureRecognizer || object == pvcTapGestureRecognizer) )
    {
        UIGestureRecognizerState state = [[change objectForKey:NSKeyValueChangeNewKey] intValue];
        switch (state)
        {
            case UIGestureRecognizerStateBegan:
            // trigger for visual feedback
            break;

            case UIGestureRecognizerStateRecognized:
            // trigger for animation-begin
            break;

            // ...
        }
    }
}

完成后,不要忘记取消订阅这些通知,否则您的应用程序可能会出现泄漏和奇怪的崩溃:

[pvcPanGestureRecognizer removeObserver:self
                             forKeyPath:@"state"];
[pvcTapGestureRecognizer removeObserver:self
                             forKeyPath:@"state"];

这就是所有的人!

于 2012-11-22T18:58:12.893 回答
4

我已经尝试了您的解决方案,它几乎可以正常工作,但仍然存在一些问题。最好的解决方案是添加方法

 - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers 

它从 iOS 6 开始可用,并且它是必需的。如果不实施它,这些手势可能会出现问题。实施它有助于解决大部分问题。

于 2012-12-13T10:27:21.233 回答
4

请首先查看我的其他答案,这个答案有严重的缺陷,但我将其留在这里,因为它可能仍然对某人有所帮助。


首先,免责声明:以下解决方案是HACK。它确实在我测试的环境中工作,但不能保证它在你的环境中工作,也不能保证它不会被下一次更新破坏。所以请谨慎行事。

TL;DR:抓住UIPanGestureRecognizerUIPageViewController劫持其委托调用,但继续将它们转发到原始目标。

更长的版本:

我对这个问题的发现:UIPageViewControlleriOS 6 中发布的行为与 iOS 5 中的不同,因为它可能会pageViewController:viewControllerBeforeViewController:在其数据源上调用或已识别出有效的方向匹配平移)。当然,这打破了我们之前的假设,即 before/after 调用等同于“动画开始”触发器,并且始终跟随pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:对委托的调用。(最终这是一个大胆的假设,但我想我并不孤单。)

我发现当UIPanGestureRecognizer页面视图控制器上的默认值开始识别最终与控制器方向不匹配的平移手势时(例如,水平分页 PVC 中的垂直平移),可能会发生对数据源的额外调用)。有趣的是,在我的环境中,受到打击的总是“之前”方法,而不是“之后”。其他人建议干扰手势识别器的委托,但这对我来说并没有像那里描述的那样工作,所以我一直在试验。

最后我找到了一个解决方法。首先我们抓取页面视图控制器的平移手势识别器:

@interface MyClass () <UIGestureRecognizerDelegate>
{
    UIPanGestureRecognizer* pvcPanGestureRecognizer;
    id<UIGestureRecognizerDelegate> pvcPanGestureRecognizerDelegate;
}
...
for ( UIGestureRecognizer* recognizer in pageViewController.gestureRecognizers )
{
    if ( [recognizer isKindOfClass:[UIPanGestureRecognizer class]] )
    {
        pvcPanGestureRecognizer = (UIPanGestureRecognizer*)recognizer;
        pvcPanGestureRecognizerDelegate = pvcPanGestureRecognizer.delegate;
        pvcPanGestureRecognizer.delegate = self;
        break;
    }
}

然后我们UIGestureRecognizerDelegate在我们的类中实现协议:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ( gestureRecognizer == pvcPanGestureRecognizer &&
        [pvcPanGestureRecognizerDelegate respondsToSelector:@selector(gestureRecognizer:shouldReceiveTouch:)] )
    {
        return [pvcPanGestureRecognizerDelegate gestureRecognizer:gestureRecognizer
                                               shouldReceiveTouch:touch];
    }
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
    shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ( gestureRecognizer == pvcPanGestureRecognizer &&
        [pvcPanGestureRecognizerDelegate respondsToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)] )
    {
        return [pvcPanGestureRecognizerDelegate gestureRecognizer:gestureRecognizer
               shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
    }
    return NO;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ( gestureRecognizer == pvcPanGestureRecognizer &&
        [pvcPanGestureRecognizerDelegate respondsToSelector:@selector(gestureRecognizerShouldBegin:)] )
    {
        return [pvcPanGestureRecognizerDelegate gestureRecognizerShouldBegin:gestureRecognizer];
    }
    return YES;
}

显然,这些方法没有做任何明智的事情,它们只是将调用转发给原始委托(确保它实际实现了它们)。尽管如此,这种转发似乎足以让 PVC 正常运行,并且在不需要时不调用数据源。

这个解决方法为我在运行 iOS 6 的设备上解决了这个问题。使用 iOS 6 SDK 编译但部署目标为 iOS 5 的代码已经在 5.x 设备上完美运行,所以修复不是必要的,但根据我的测试它也没有任何伤害。

希望有人觉得这很有用。

于 2012-11-07T10:55:25.213 回答
0

尝试这个...

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
 {   

        for (UIGestureRecognizer *gr in pageViewController.gestureRecognizers) {
                if([gr isKindOfClass:[UIPanGestureRecognizer class]])
                {
                    UIPanGestureRecognizer *pgr = (UIPanGestureRecognizer*)gr;
                    CGPoint velocity = [pgr velocityInView:pageViewController.view];
                    BOOL verticalSwipe = fabs(velocity.y) > fabs(velocity.x);
                    if(verticalSwipe)
                        return nil;
                }
            }
    ....
}
于 2017-07-18T12:19:40.557 回答