在 UIContainerView 中动画 2 UIViewControllers
Animate 2 UIViewControllers in a UIContainerView
大家好,我正在为 UIContainerView 中的 2 个 UIViewController 的显示设置动画。为了展示视图控制器,我实现了这个方法
#pragma mark - Animation Controller
-(void)moveViewControllerFrom:(UIViewController *)currentViewController toViewController:(UIViewController *)nextViewController {
CGFloat widthVC = self.containerView.frame.size.width;
CGFloat heightVC = self.containerView.frame.size.height;
if (currentViewController == nextViewController) return;
if (_isNextViewController) {
nextViewController.view.frame = CGRectMake(widthVC, 0, widthVC, heightVC);
[self addChildViewController:nextViewController];
[currentViewController willMoveToParentViewController:nil];
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:.4 options:0 animations:^{
nextViewController.view.frame = currentViewController.view.frame;
currentViewController.view.frame = CGRectMake(0 - widthVC, 0, widthVC, heightVC);
}
completion:^(BOOL finished) {
[currentViewController removeFromParentViewController];
[nextViewController didMoveToParentViewController:self];
}];
}
else {
nextViewController.view.frame = CGRectMake(0 -widthVC, 0, widthVC, heightVC);
[self addChildViewController:nextViewController];
[currentViewController willMoveToParentViewController:nil];
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:.4 options:0 animations:^{
nextViewController.view.frame = currentViewController.view.frame;
currentViewController.view.frame = CGRectMake(widthVC, 0, widthVC, heightVC);
}
completion:^(BOOL finished) {
[currentViewController removeFromParentViewController];
[currentViewController didMoveToParentViewController:self];
}];
}
}
现在我在快速按下前进和后退按钮(我用来显示新视图控制器或返回到前一个视图控制器的按钮)时遇到了问题。我的应用程序 crasha 和 consol returns 这个错误给我:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Children view controllers
and
must have a common parent
view controller when calling -[UIViewController
transitionFromViewController:toViewController:duration:options:animations:completion:]'
有人可以帮助我了解我的代码哪里出错了吗?
当您第二次调用 moveViewController:toViewController
时,您的 currentViewController
可能尚未完全添加到 self
。您可以在方法的开头放置这样的语句,看看我的假设是否正确:
NSAssert([self.childViewControllers containsObject:currentViewController], @"The currentViewController: %@ is not a childViewController, here are the current childViewControllers: %@", currentViewController, self.childViewControllers);
编辑:
如果这个假设不正确,编辑您的问题以包括您对该方法的调用以获得进一步的上下文可能会有所帮助
大家好,我正在为 UIContainerView 中的 2 个 UIViewController 的显示设置动画。为了展示视图控制器,我实现了这个方法
#pragma mark - Animation Controller
-(void)moveViewControllerFrom:(UIViewController *)currentViewController toViewController:(UIViewController *)nextViewController {
CGFloat widthVC = self.containerView.frame.size.width;
CGFloat heightVC = self.containerView.frame.size.height;
if (currentViewController == nextViewController) return;
if (_isNextViewController) {
nextViewController.view.frame = CGRectMake(widthVC, 0, widthVC, heightVC);
[self addChildViewController:nextViewController];
[currentViewController willMoveToParentViewController:nil];
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:.4 options:0 animations:^{
nextViewController.view.frame = currentViewController.view.frame;
currentViewController.view.frame = CGRectMake(0 - widthVC, 0, widthVC, heightVC);
}
completion:^(BOOL finished) {
[currentViewController removeFromParentViewController];
[nextViewController didMoveToParentViewController:self];
}];
}
else {
nextViewController.view.frame = CGRectMake(0 -widthVC, 0, widthVC, heightVC);
[self addChildViewController:nextViewController];
[currentViewController willMoveToParentViewController:nil];
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:.4 options:0 animations:^{
nextViewController.view.frame = currentViewController.view.frame;
currentViewController.view.frame = CGRectMake(widthVC, 0, widthVC, heightVC);
}
completion:^(BOOL finished) {
[currentViewController removeFromParentViewController];
[currentViewController didMoveToParentViewController:self];
}];
}
}
现在我在快速按下前进和后退按钮(我用来显示新视图控制器或返回到前一个视图控制器的按钮)时遇到了问题。我的应用程序 crasha 和 consol returns 这个错误给我:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Children view controllers and must have a common parent view controller when calling -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]'
有人可以帮助我了解我的代码哪里出错了吗?
当您第二次调用 moveViewController:toViewController
时,您的 currentViewController
可能尚未完全添加到 self
。您可以在方法的开头放置这样的语句,看看我的假设是否正确:
NSAssert([self.childViewControllers containsObject:currentViewController], @"The currentViewController: %@ is not a childViewController, here are the current childViewControllers: %@", currentViewController, self.childViewControllers);
编辑: 如果这个假设不正确,编辑您的问题以包括您对该方法的调用以获得进一步的上下文可能会有所帮助