Get the final frames of the subcell during the transition of the view controller

I am implementing a custom transition between view controllers in iOS 7+. In particular, I have the same button as controller A and controller B, the only difference is in its position (it is slightly higher in the view of controller B).

I want that when moving from the controller A of the view to view the controller B, the button from A moves so that it ends in the end position in the form of controller B.

Here is my animateTransition: method:

 - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView *containerView = [transitionContext containerView]; [containerView insertSubview:toViewController.view belowSubview:fromViewController.view]; UIButton *fromButton = (UIButton *)[containerView viewWithTag:1]; UIButton *toButton = (UIButton *)[containerView viewWithTag:2]; toButton.hidden = YES; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ fromButton.center = toButton.center; } completion:^(BOOL finished) { toButton.hidden = NO; [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; } 

The problem that I see is that toButton.center NOT the correct position. It, as well as AutoLayout restrictions, has not yet been applied (I get the same frame as in IB). This leads to the fact that after the transition is completed, the button does not end in the correct position.

Where is the mistake? How can I get the correct final presentation frames from the presented view controller? If this is not possible because view controller B has not yet appeared, how can I animate a subview to achieve this effect?

+6
source share
2 answers

You can call layoutIfNeeded () in the toViewController view before starting the animation. This should place your subitems and provide you with the final frame for your toButton.

+6
source

Instead of setting the absolute center, adjust the center to the delta.

0
source

Source: https://habr.com/ru/post/982533/


All Articles