App crashes if view controller transition is done

2019-07-21 02:56发布

I am using view controller containment to transition between 6 view controllers. Transitions are controlled using a segmented control. This all works fine unless buttons on the segmented control are pushed before animation of the previous transition has completed. In this situation, the app crashes with

'Children view controllers and must have a common parent view controller when calling -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]'

Code is:

[self transitionFromViewController:currentVC
                  toViewController:newVC
                          duration:1.0
                           options:UIViewAnimationOptionTransitionFlipFromRight
                        animations:nil
                        completion:^(BOOL finished) {
                            [currentVC removeFromParentViewController];
                            [newVC didMoveToParentViewController:self];
                            currentVC = newVC;
                        }];

Should I disable the segmented control until animation is completed? Or is their a better way to avoid this issue?

1条回答
做个烂人
2楼-- · 2019-07-21 03:41

You can disable and reenable application interaction by calling

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];

when the animation starts and ends respectively. The app will then ignore all interaction (touch events) until the animation is finished, so the segment will never receive event before it is safe (animations are done).

I think this approach is used on some built-in container controllers as well. However be careful about animation duration. If the animation will take a long time, it can look like the app is not responding well, which hurts user experience

查看更多
登录 后发表回答