[英]Unbalanced calls to begin/end appearance transitions for <UIViewController>
Based on the Apple documentation I came up with the following method to switch between controllers in a containment controller. 根据Apple文档,我提出了以下方法来在包容控制器中切换控制器。 When there is an oldC
I am getting Unbalanced calls to begin/end appearance transitions for <...>
on the console. 当有一个oldC
我在控制台上Unbalanced calls to begin/end appearance transitions for <...>
。
- (void) showController:(UIViewController*)newC withView:(UIView*)contentView animated:(BOOL)animated
{
UIViewController *oldC = self.childViewControllers.firstObject;
if (oldC == newC) {
return;
}
[oldC willMoveToParentViewController:nil];
[self addChildViewController:newC];
newC.view.frame = (CGRect){ 0, 0, contentView.frame.size };
[contentView addSubview:newC.view];
if (animated && oldC != nil) {
oldC.view.alpha = 1.0f;
newC.view.alpha = 0.0f;
[self transitionFromViewController:oldC toViewController:newC duration:0.25f options:0 animations:^{
oldC.view.alpha = 0.0f;
newC.view.alpha = 1.0f;
} completion:^(BOOL finished) {
[oldC removeFromParentViewController];
[newC didMoveToParentViewController:self];
}];
} else {
oldC.view.alpha = 0.0f;
newC.view.alpha = 1.0f;
[oldC removeFromParentViewController];
[newC didMoveToParentViewController:self];
}
}
This is how I call it: 这就是我所说的:
- (IBAction) buttonSignIn:(id)sender
{
[self showController:self.signInViewController withView:self.contentView animated:(sender != nil)];
}
- (IBAction) buttonSignUp:(id)sender
{
[self showController:self.signUpViewController withView:self.contentView animated:(sender != nil)];
}
To track this down I am logging the appearance transitions 为了追踪这一点,我正在记录外观过渡
-(void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated
{
[super beginAppearanceTransition:isAppearing animated:animated];
NSLog(@"**begin %@", self);
}
-(void)endAppearanceTransition
{
[super endAppearanceTransition];
NSLog(@"**end** %@", self);
}
This is what the log looks like: 这就是日志的样子:
] **begin <SignInViewController: 0x10c769a20>
] **begin <SignUpViewController: 0x10c768770>
] Unbalanced calls to begin/end appearance transitions for <SignUpViewController: 0x10c768770>.
] **end** <SignUpViewController: 0x10c768770>
] **end** <SignInViewController: 0x10c769a20>
Now I am a little puzzled. 现在我有点困惑。 What's the problem here? 这有什么问题?
Turns out transitionFromViewController:toViewController:duration:options:animations:completion:
also adds the view. 结果是transitionFromViewController:toViewController:duration:options:animations:completion:
还添加了视图。
This method adds the second view controller's view to the view hierarchy and then performs the animations defined in your animations block. 此方法将第二个视图控制器的视图添加到视图层次结构,然后执行动画块中定义的动画。 After the animation completes, it removes the first view controller's view from the view hierarchy. 动画完成后,它将从视图层次结构中删除第一个视图控制器的视图。
Which means the addSubview
needs to be adjusted accordingly. 这意味着需要相应地调整addSubview
。
- (void) showController:(UIViewController*)newC withView:(UIView*)contentView animated:(BOOL)animated
{
UIViewController *oldC = self.childViewControllers.firstObject;
if (oldC == newC) {
return;
}
[oldC willMoveToParentViewController:nil];
[self addChildViewController:newC];
newC.view.frame = (CGRect){ 0, 0, contentView.frame.size };
if (animated && oldC != nil) {
oldC.view.alpha = 1.0f;
newC.view.alpha = 0.0f;
[self transitionFromViewController:oldC toViewController:newC duration:0.25f options:0 animations:^{
oldC.view.alpha = 0.0f;
newC.view.alpha = 1.0f;
} completion:^(BOOL finished) {
[oldC removeFromParentViewController];
[newC didMoveToParentViewController:self];
}];
} else {
[contentView addSubview:newC.view];
oldC.view.alpha = 0.0f;
newC.view.alpha = 1.0f;
[oldC removeFromParentViewController];
[newC didMoveToParentViewController:self];
}
}
old code 旧代码
[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];
[self.view addSubview:toVC.view];
[toVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(0);
}];
[self transitionFromViewController:fromVC
toViewController:toVC duration:0
options:UIViewAnimationOptionTransitionNone
animations:^{
} completion:^(BOOL finished) {
[fromVC.view removeFromSuperview];
[fromVC removeFromParentViewController];
[toVC didMoveToParentViewController:self];
self.currentVC = toVC;
}];
new code, it's ok 新代码,没关系
[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];
[self transitionFromViewController:fromVC
toViewController:toVC duration:0
options:UIViewAnimationOptionTransitionNone
animations:^{
} completion:^(BOOL finished) {
[fromVC.view removeFromSuperview];
[fromVC removeFromParentViewController];
[toVC didMoveToParentViewController:self];
self.currentVC = toVC;
}];
我将持续时间设置为0,这意味着animation:NO
对于transitionFromViewController
调用为animation:NO
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.