ios - UIPageControl 当前页面指示器在呈现新 View 之前切换

标签 ios objective-c iphone uipageviewcontroller uipagecontrol

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

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

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

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

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

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

编辑

这是我用来实现 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 bool 值,告诉您是否应该更新您的页面控件。您当前的实现似乎有问题,因为一旦您开始滑动页面,它就会加载下一页的 View Controller 并调用 ...didShow... 方法。希望这会有所帮助。

关于ios - UIPageControl 当前页面指示器在呈现新 View 之前切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30324011/

相关文章:

ios - 如何为数组中的每个创建一个具有唯一图像和选择器的自定义 UIButton?

objective-c - 如何以编程方式在 iOS 中按下 "dictate"键盘键(Objective-C)

android - 应用内结算到期

ios - 为什么身份验证不适用于我的 ASP.NET REST API?

iphone - 旋转 View 时不触发 UIInterfaceOrientationIsLandscape

ios - 更改文本字段和 "save before quit"

iphone - 在iTunes Connect中输入元数据之前或之后,我是否必须通过Xcode 4存档共享功能将更新提交到我的应用程序?

iphone - 如何使拖动的 UIView 减速?

ios - 使用#define,好的做法?

ios - 如何在 Objective C 中单击按钮时更新 UILabel 的 centerXAnchor