这就是我使用包含API的方式.根据
docs是正确的.
[self.childViewController willMoveToParentViewController:nil]; [UIView animateWithDuration:0.25 animations:^{ self.childViewController.view.frame = ... // View Animation } completion:^(BOOL finished) { [self.childViewController.view removeFromSuperview]; // triggers `viewWillDisappear` [self.childViewController removeFromParentViewController]; }];
我希望在动画开始之前调用viewWillDisappear,并在动画完成后调用viewDidDisappear.但是,在动画完成后,它们都会快速连续调用.
移动[self.childViewController.view removeFromSuperview];到动画块修复了这个,但代码看起来错了:
[self.childViewController willMoveToParentViewController:nil]; [UIView animateWithDuration:0.25 animations:^{ self.childViewController.view.frame = ... // View Animation [self.childViewController.view removeFromSuperview]; // triggers `viewWillDisappear` } completion:^(BOOL finished) { [self.childViewController removeFromParentViewController]; }];
解决方法
答案是使用
– beginAppearanceTransition:animated:
& endAppearanceTransition
If you are implementing a custom container controller,use this method to tell the child that its views are about to appear or disappear. Do not invoke viewWillAppear:,viewWillDisappear:,viewDidAppear:,or viewDidDisappear: directly.
更正后的代码:
[self.childViewController willMoveToParentViewController:nil]; [self.childViewController beginAppearanceTransition:NO animated:YES]; [UIView animateWithDuration:0.25 animations:^{ self.childViewController.view.frame = ... } completion:^(BOOL finished) { [self.childViewController.view removeFromSuperview]; [self.childViewController removeFromParentViewController]; [self.childViewController endAppearanceTransition]; }];