自定义视图控制器转换iOS 7期间闪烁

我正在尝试进行一个非常简单的过渡:一个视图将屏幕的一半移动到左侧,而第二个(“到”)视图移动半个屏幕.

我有动画工作,但当我反转动画时,我看到一个闪烁. “to”视图(即原始视图)在0,0的原点可见,尽管我设置了不同的帧.

我转储了视图层次结构.帧设置正确(-100 0; 320 480用于查看),但是它显示为0,0.视图的屏幕截图是否缓存在动画的某处?

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController   = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

UIView *container = [transitionContext containerView];

CGRect offsetCoverRect    = CGRectMake(-100.0,0.0,320,480);
CGRect detailsRect        = CGRectMake(-100.0 + 320.0,480);
CGRect detailsOutsideRect = CGRectMake(320.0,480);

CGRect normalRect = CGRectMake(0.0,480);


if (self.revealDetails)
{
    toViewController.view.frame = detailsOutsideRect;
    [container addSubview:toViewController.view];
}
else
{
    [container insertSubview:toViewController.view belowSubview:fromViewController.view];

    // reversing… set the frame to the original offset (shows at 0,0 for a moment)
    toViewController.view.frame = offsetCoverRect;
}

[UIView animateKeyframesWithDuration:2 delay:0 options:0 animations:^{

    if (self.revealDetails)
    {
        fromViewController.view.frame = offsetCoverRect;
        toViewController.view.frame = detailsRect;
    }
    else
    {
        fromViewController.view.frame = detailsOutsideRect;
        toViewController.view.frame = normalRect;
    }
} completion:^(BOOL finished) { [transitionContext completeTransition:finished]; }];
}

更新:

它似乎与UIModalPresentationCustom有关.我需要使用它,以便在转换完成时不删除from视图.但是,它似乎假设反向转换的from视图控制器从0,0开始.

更新2:

使用以下代码非常容易重现:

UIView *snapshot = [containerView snapshotViewAfterScreenUpdates:NO];
[containerView addSubview:snapshot];

以上将显示以屏幕为中心的视图,无论我在动画之前设置的实际帧或中心.

解决方法

我知道这是一个老问题,但我遇到了同样的问题,经过几个小时的挣扎,终于想出了一个解决方案:

在本动画的完成块中,

>创建fromViewController视图的快照
>对快照执行帧更改/转换
>将快照添加到containerView
>从容器视图中删除fromViewController视图
>在解雇动画的完成块中(或者在解除动画的任何地方?),从容器中删除快照

通过从容器视图中删除fromViewController的视图,它不会重置为它的初始位置,因为它不再位于容器视图中.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...