iOS how to swap cycle 3 view controllers through 2 containers with animation?

168 Views Asked by At

How I can sequentially recycle 3 view controllers using 2 containers with animation and blocks? Here's the pattern I'm trying to implement:

    2
    ^
2 > 1
1 > 3 //reassign pointers and repeat
^
3

I have 3 view controllers setup, ready for swapping. The inital swap seems to work, but I'm not really sure what I have to do within the second completion block if I'm recycling and not re-instantiating view controllers.

I get exception:

'Children view controllers x and y must have a common parent view controller when calling -

Here's how I set the code up

-(void)setup
{
    item1 = (ItemStatsViewController*)[mainStoryboard  instantiateViewControllerWithIdentifier: @"item"];
    item2 = (ItemStatsViewController*)[mainStoryboard  instantiateViewControllerWithIdentifier: @"item"];
    item3 = (ItemStatsViewController*)[mainStoryboard  instantiateViewControllerWithIdentifier: @"item"];


    [self addChildViewController:item1];
    [self addChildViewController:item2];
    [self addChildViewController:item3];

    item1.view.frame=self.item1Container.bounds;
    item2.view.frame=self.item2Container.bounds;

    [self.item1Container addSubview:item1.view];
    [self.item2Container addSubview:item2.view];


    [item1 didMoveToParentViewController:self];
    [item2 didMoveToParentViewController:self];
}

This method attempts to cycle them

-(void)swapViewControllers{

    [item3.view layoutIfNeeded];

    item3.view.frame=self.item1Container.bounds;
    [item1 willMoveToParentViewController:nil];


    __weak __block InventoryViewController *weakSelf=self;
    [self transitionFromViewController:item1
                      toViewController:item3
                              duration:1.0
                               options:UIViewAnimationOptionTransitionFlipFromBottom
                            animations:nil
                            completion:^(BOOL finished) {

                                [item1 removeFromParentViewController];
                                [item3 didMoveToParentViewController:weakSelf];

                                [item1.view layoutIfNeeded];
                                item1.view.frame=self.item2Container.bounds;

                                [item2 willMoveToParentViewController:nil];
                                [self addChildViewController:item2];

                                [self transitionFromViewController:item2
                                                  toViewController:item1
                                                          duration:1.0
                                                           options:UIViewAnimationOptionTransitionFlipFromBottom
                                                        animations:nil
                                                        completion:^(BOOL f) {

                                                            [item2 removeFromParentViewController];
                                                            [item1 didMoveToParentViewController:weakSelf];



                                                            ItemStatsViewController* temp = item2;
                                                            item2 = item3;
                                                            item3 = item1;
                                                            item1 = temp;

                                                        }];


                            }];
}
0

There are 0 best solutions below