这就是我使用包含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];
}];
有谁知道在正确的时间调用viewWilldisappear的正确方法是什么?
解决方法
答案是使用
– 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];
}];