UIPageControl 当前页面指示器在呈现新视图之前切换

UIPageControl Current page indicator switches before new view is presented

我用 PageControl 实现了一个很好的UIPageViewControl。滑动指示器时会发生变化并显示正确的当前页面。

但是我注意到,当前页面指示器切换所需的只是开始滑动。这意味着如果我开始滑动但随后松开手指,则当前页面指示器已切换,就像页面已切换一样,但事实并非如此。这是我用来进行切换的代码:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    NSUInteger index = [self.navigationController.viewControllers indexOfObject:viewController];
    self.pageControl.currentPage = index;
}

我注意到的另一件事是,当我快速左右滑动更改视图时,页面指示器只是卡住并且不会移动。

因此它仅在您不快速更改视图时才有效。如果您需要任何其他代码,请告诉我。谢谢。

编辑

这是我用来实现 UIPageViewController 的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create the data model

    self.navigationItem.title = @"Tabell";
    self.identifiers = [[NSMutableArray alloc] init];

    [self.identifiers addObject:@"rankTable"];
    [self.identifiers addObject:@"page2"];

    // Create page view controller
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;
    self.pageViewController.delegate = self;


    self.navigationController.delegate = self;
    CGSize navBarSize = self.navigationController.navigationBar.bounds.size;
    CGPoint origin = CGPointMake( navBarSize.width/2, navBarSize.height/2 );
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(origin.x, origin.y+16,
                                                                       0, 0)]; //Here added 45 to Y and it did the trick


    self.pageControl.pageIndicatorTintColor = navbarColor;
    self.pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];

    [self.pageControl setNumberOfPages:2];

    [self.navigationController.navigationBar addSubview:self.pageControl];

    UITableViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

     CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;

    // Change the size of page view controller
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - tabBarHeight);

    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];

}

你为什么不实施 UIPageViewControllerDelegate

- pageViewController: didFinishAnimating: previousViewControllers: transitionCompleted:方法?它有一个 transitionCompleted 布尔值,告诉您是否应该更新您的页面控件。您当前的实现似乎有问题,因为一旦您开始滑动页面,它就会加载下一页的视图控制器并调用 ...didShow... 方法。希望这个hepls.