【问题标题】:Get final frames of subviews while in a view controller transition在视图控制器转换中获取子视图的最终帧
【发布时间】:2015-02-15 23:46:09
【问题描述】:

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

我希望在从视图控制器 A 到视图控制器 B 的转换中,来自 A 的按钮向上移动,以便它在视图控制器 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 中相同的框架矩形)。这会导致转换完成后按钮没有在正确的位置结束。

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

【问题讨论】:

  • 你找到解决这个问题的方法了吗?我也面临同样的问题。
  • @Alex 不,还没有解决方案。

标签: ios uiviewcontroller


【解决方案1】:

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

【讨论】:

  • 这个,先把 setNeedsLayout() 粘贴进去,你就可以开始了。
【解决方案2】:

与其设置绝对中心,不如将中心调整一个增量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多