在视图控制器转换中获取子视图的最终帧 [英] Get final frames of subviews while in a view controller transition

查看:23
本文介绍了在视图控制器转换中获取子视图的最终帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 iOS 7+ 中的视图控制器之间实现自定义转换.特别是,我在视图控制器 A 和视图控制器 B 中有相同的按钮,唯一的区别是它的位置(它在视图控制器 B 中高一点).

I'm implementing a custom transition between view controllers in iOS 7+. In particular, I have the same button in view controller A and view controller B, the only difference is in its position (it's a little bit higher in view controller B).

我希望在从视图控制器 A 到视图控制器 B 的过渡中,来自 A 的按钮向上移动,以便它在视图控制器 B 中的最终位置结束.

I want that in the transition from view controller A to view controller B the button from A moves up so that it ends in the final position in view controller B.

这是我的 animateTransition: 方法:

- (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]];
    }];
}

我看到的问题是 toButton.center 不是正确的位置.这就像尚未应用 AutoLayout 约束(我得到与 IB 中相同的框架矩形).这会导致转换完成后按钮没有在正确的位置结束.

The problem I see is that toButton.center is NOT the correct position. It's like AutoLayout constraints have not been applied yet (I get the same frame rect as in IB). This causes that the button does not end in the correct position after the transition finishes.

错误在哪里?如何从呈现的视图控制器中获取正确的子视图最终帧?如果由于视图控制器 B 尚未出现而无法实现,我如何为子视图设置动画以实现此效果?

Where's the error? How can I get the correct final frames of subviews from the presented view controller? If that's not possible because view controller B has not appeared yet, how can I animate subviews to achieve this effect?

推荐答案

在开始动画之前,您可以在 toViewController 的视图上调用 layoutIfNeeded().这应该布局您的子视图并为您的 toButton 提供最终框架.

You can call layoutIfNeeded() on your toViewController's view before you start your animation. That should layout your subviews and give you the final frame for your toButton.

这篇关于在视图控制器转换中获取子视图的最终帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆