Вопрос

I want a segmented control to act like the gray dots at the bottom of the page of a pageview controller. So, when I swipe to the right, it moves to the segment on the right, and when I swipe to the left, it moves to the segment on the left. I've already got it to change the page when I click on a specific segment correctly (so if I click the first segment, it moves to the first page). However, I can't seem to make the code do exactly what I want it to do. Right now I have the following code:

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

This works fine, but the problem is that this is called when the transition is happening, not when it is finished. Therefore, when I start changing the page, but then stop, it moves the segment over. So, I tried some other code:

- (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;
    }
}

This is called at the right time. If I put an NSLOG, it shows up ever time I finish a transition, which is exactly what I want, but for some reason, the segmentcontrol is lagged behind by 1 move. So, if I go from page 1 to page 2, segment stays at 1, and then if i go from page 2 to page 3, segment moves to 2, and then from then on it continues to lag behind by one move no matter which direction I go. I've played around with it and I can't seem to get the logic straight. Help would be greatly appreciated!!

Here are the helper functions:

- (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;
}

Any of the commented stuff was stuff I was playing with to try to get the logic straight. I just ran the program with didFinishAnimating running and willTransitionToViewControllers commented out. This gave the lagged effect where the segmented control is one behind the cells. Any help is very appreciated

Это было полезно?

Решение

this is how i solved it:

- (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;
    }
}

Essentially, I used both the willTransition and the didFinishAnimation. When you call a transition, the method changes a global variable, pageIndex, to get the page index, but doesn't change the segment. When you finish the animation, the didFinish method pulls the global pageIndex, and sets the segment to be that page.

Другие советы

You could just use didset method:

var pageIndex:Int!{
     didSet{
     segmentControl.selectedSegmentIndex = pageIndex
}
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top