在旋转期间更新 UIPageViewController 时不会调用完成块

Hej*_*azi 6 objective-c screen-rotation ios uipageviewcontroller

我正在尝试UIPageViewController在轮换期间更新当前页面,如下所示:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSArray *controllers = @[someViewController];
    UIPageViewControllerNavigationDirection direction = UIPageViewControllerNavigationDirectionForward;

    [self.pageController setViewControllers:controllers direction:direction animated:YES completion:^(BOOL finished) {
        /// This completion block isn't called ever in this case.
    }];
}
Run Code Online (Sandbox Code Playgroud)

但是,出于某种原因,在这种情况下永远不会调用完成块,而当我从其他地方使用相同的参数调用相同的方法时,它会按预期调用。

任何人都可以确认这种奇怪的行为,如果这是一个错误UIPageViewController还是只是对我的误用?

更新

我发现,如果我改变参数animated,以NO如预期完成块将被称为!因此,在我看来,它是UIPageViewController.

小智 7

有同样的问题,发现了以下事情:如果someViewController是当前可见的控制器或pageController.viewControllers?.firstanimated标志设置为true,则setViewControllers称为不调用完成处理程序。最简单的方法是调用setViewControllers带有animated标志设置为false。不会显示任何动画,但无论如何都会调用完成处理程序。但在大多数情况下,您需要显示动画。因此,这里唯一可能的解决方法是检查要设置的 viewController 是否不是可见的。如果是,您应该setViewControllersanimated标志设置为 false 的情况下调用:

let completionBlock: (Bool) -> Void
let isAnimated = (newController != pageController.viewControllers?.first)

pageController.setViewControllers([newController],
                                  direction: .forward,
                                  animated: isAnimated, 
                                  completion: completionBlock)
Run Code Online (Sandbox Code Playgroud)

希望能帮到你!


Imr*_*med -2

尝试如下:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self newMethod];
  // or  [self performSelector:@selector(newMethod) withObject:self afterDelay:0.01];
}

- (void)newMethod{
    NSArray *controllers = @[someViewController];
    UIPageViewControllerNavigationDirection direction = UIPageViewControllerNavigationDirectionForward;

    [self.pageController setViewControllers:controllers direction:direction animated:YES          completion:^(BOOL finished) {
        ///
    }];
}
Run Code Online (Sandbox Code Playgroud)