如何使用带有动画和块的2个容器顺序回收3个视图控制器?以下是我试图实现的模式:
2
^
2 > 1
1 > 3 //reassign pointers and repeat
^
3
我有3个视图控制器设置,可以进行交换。初始交换似乎有效,但我不确定在第二个完成块中我必须做什么,如果我正在回收而不是重新实例化视图控制器。
我得到例外:
'Children view controllers x and y must have a common parent view controller when calling -
以下是我设置代码的方法
-(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];
}
此方法试图循环它们
-(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;
}];
}];
}