簡體   English   中英

自定義視圖控制器轉換iOS 7期間閃爍

[英]Flickering during custom view controller transition 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, 0.0, 320, 480);
CGRect detailsOutsideRect = CGRectMake(320.0, 0.0, 320, 480);

CGRect normalRect = CGRectMake(0.0, 0.0, 320, 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];

以上將顯示以屏幕為中心的視圖,無論我在動畫之前設置的實際幀或中心。

我知道這是一個老問題,但我遇到了同樣的問題,經過幾個小時的掙扎,終於想出了一個解決方案:

在本動畫的完成塊中,

  1. 創建fromViewController視圖的快照
  2. 在快照上執行幀更改/轉換
  3. 將快照添加到containerView
  4. 從容器視圖中刪除fromViewController視圖
  5. 在解雇動畫的完成塊中(或者在解除動畫的任何地方?),從容器中刪除快照

通過從容器視圖中刪除fromViewController的視圖,它不會重置為它的初始位置,因為它不再位於容器視圖中。

這是因為您的animateTransition方法是從后台調用的。 以這種方式調用您的方法

dispatch_async(dispatch_get_main_queue(), ^
               {
                   [self animateTransition]; // your method goes here
               });

您永遠不應該在后台線程上調用任何動畫或任何UI相關方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM