gpt4 book ai didi

ios - 将 UIPageViewController 与 UISegmentedControl 逻辑问题同步

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:39 25 4
gpt4 key购买 nike

我希望分段控件的行为类似于页面 View Controller 页面底部的灰点。因此,当我向右滑动时,它会移动到右侧的部分,而当我向左滑动时,它会移动到左侧的部分。当我正确点击特定的部分时,我已经知道它可以更改页面(所以如果我点击第一个部分,它会移动到第一页)。但是,我似乎无法让代码完全按照我的意愿去做。现在我有以下代码:

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
if([pendingViewControllers count]>0)
{
NSUInteger index =[(MasterTableViewController*)[pendingViewControllers objectAtIndex:0] pageIndex];
segmentedControl.selectedSegmentIndex = index;
}
}

这工作正常,但问题是它是在转换发生时调用的,而不是在转换完成时调用的。因此,当我开始更改页面但随后停止时,它会移动该段。所以,我尝试了一些其他代码:

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if(finished && completed && [previousViewControllers count]>0)
{
NSUInteger index =[(MasterTableViewController*)[previousViewControllers objectAtIndex:0] pageIndex];
segmentedControl.selectedSegmentIndex = index;
self.lastSelected = index;
}
}

这是在正确的时间调用的。如果我放置一个 NSLOG,它会在我完成转换时显示,这正是我想要的,但由于某种原因,段控制落后了 1 步。所以,如果我从第 1 页转到第 2 页,段保持在 1,然后如果我从第 2 页转到第 3 页,段移动到 2,然后从那时起它继续落后一步,无论哪个我去的方向。我玩过它,但似乎无法弄清楚逻辑。帮助将不胜感激!!

以下是辅助函数:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((MasterTableViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}

index--;
return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((MasterTableViewController*) viewController).pageIndex;

if (index == NSNotFound) {
return nil;
}
index++;
// startingViewController.pageIndex = index;
if (index == [self.pageTitles count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}

- (MasterTableViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
return nil;
}
MasterTableViewController *pageContentViewController;

if (index == 0)
{
pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:@"OnCampusTable"];
}
else if (index == 1)
{
pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:@"OffCampusTable"];
}
else if (index == 2)
{
pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyEventsTable"];
}
// pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.pageIndex = index;

return pageContentViewController;
}

任何评论的东西都是我用来弄清楚逻辑的东西。我只是在运行 didFinishAnimating 的情况下运行程序,并将 willTransitionToViewControllers 注释掉。这产生了滞后效应,其中分段控制是细胞后面的一个。非常感谢任何帮助

最佳答案

我是这样解决的:

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
if([pendingViewControllers count]>0)
{
NSUInteger index =[(MasterTableViewController*)[pendingViewControllers objectAtIndex:0] pageIndex];
startingViewController.pageIndex = index;
}
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if(finished && completed && [previousViewControllers count]>0)
{
NSUInteger index =[(MasterTableViewController*)[previousViewControllers objectAtIndex:0] pageIndex];
segmentedControl.selectedSegmentIndex = startingViewController.pageIndex;
self.lastSelected = index;
}
}

基本上,我同时使用了 willTransition 和 didFinishAnimation。当您调用转换时,该方法会更改全局变量 pageIndex 以获取页面索引,但不会更改段。当您完成动画时,didFinish 方法会拉取全局 pageIndex,并将段设置为该页面。

关于ios - 将 UIPageViewController 与 UISegmentedControl 逻辑问题同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21562078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com