使用一个动画

时间:2017-01-24 12:13:29

标签: ios uiviewcontroller presentviewcontroller

问题是我不知道如何解雇并呈现只有一个过渡动画的视图控制器。

我的故事板结构是:

enter image description here 我们可以说 A 控制器在NavigationController之后, B 是启动参考, C 是TabBar ViewController。 B C 都以模态方式呈现 Cross Dissolve 转换。

当用户登录应用时(来自 B ), C 控制器将以模态方式显示 Flip horizo​​ntal 转换。当用户注销(来自 C )时, B 以相同的方式显示。 在 A 控制器上,我执行直接转换为 B C ,具体取决于用户是否已登录。

我的问题是,如果我不从 B C 中删除以前的视图控制器,则该控制器会泄露。相反,如果我将其解雇,则会在目标控制器( B C )出现之前显示 A

是否可以仅显示水平翻转过渡并跳过 A 视图?

3 个答案:

答案 0 :(得分:3)

我对此问题的解决方案是替换当前的rootViewController,支持不同的转换:

static func replaceRootViewController(with viewController: UIViewController, transition: UIViewAnimationOptions, completion: (() -> ())? = nil) {        
    if transition == .transitionCrossDissolve {
        let overlayView = UIScreen.main.snapshotView(afterScreenUpdates: false)
        viewController.view.addSubview(overlayView)
        UIApplication.shared.keyWindow?.rootViewController = viewController

        UIView.animate(withDuration: 0.65, delay: 0, options: transition, animations: {
            overlayView.alpha = 0
        }, completion: { finished in
            overlayView.removeFromSuperview()
            if let completion = completion{
                completion()
            }
        })
    } else {
        _ = viewController.view
        UIView.transition(with: UIApplication.shared.keyWindow!, duration: 0.65,options: transition, animations: {
            UIApplication.shared.keyWindow?.rootViewController = viewController
        }){_ in
            if let completion = completion {
                completion()
            }

        }
    }
}

答案 1 :(得分:2)

这是我用于此问题的解决方案。我不知道它是如何与Storyboard集成的,因为我不使用它们。

在UIViewController的类别中添加了此方法,然后可以调用以前称为presentViewController:animated:completion的任何位置。导致新控制器的无缝动画,同时仍然消除前一个控制器。

-(void)presentViewControllerDismissingPrevious:(UIViewController* _Nonnull)controller animated:(BOOL)animated completion:(void (^ __nullable)(void))completion {

    UIViewController* visibleController = self;
    {
        UIViewController* temp;
        while( ( temp = visibleController.presentedViewController ) != nil ) {
            visibleController = temp;
        }
    }

    if( visibleController == self ) {
        // no previous controller to dismiss
        [self presentViewController:controller animated:animated completion:completion];
    } else {
        // create a temporary snapshot of the visible controller's entire window
        // and add to the current view controller's window until animation completed
        UIWindow* visibleWindow = visibleController.view.window;
        UIView* tempView = [visibleWindow snapshotViewAfterScreenUpdates:NO];
        UIView* rootView = self.view.window.subviews[0];
        tempView.frame = [rootView convertRect:visibleWindow.bounds fromView:visibleWindow];
        [rootView addSubview:tempView];

        [self dismissViewControllerAnimated:NO completion:^(){
            [self presentViewController:controller animated:animated completion:^(){
                [tempView removeFromSuperview];
                if( completion ) {
                    completion();
                }
            }];
        }];
    }
}

答案 2 :(得分:2)

两个答案都非常有用,这是 Swift 4 版本:

func presentHidingBehindScreenSnapshot(viewController: UIViewController,
                                     completion: (() -> (Void))? ) {
if let screenSnapshot = UIApplication.shared.keyWindow?.snapshotView(afterScreenUpdates: false),
  let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
  rootViewController.view.addSubview(screenSnapshot)
  rootViewController.view.bringSubview(toFront: screenSnapshot)

  rootViewController.dismiss(animated: false, completion: {
    rootViewController.present(viewController, animated: false, completion: {
      screenSnapshot.removeFromSuperview()
      if let existingCompletion = completion {
        existingCompletion()
      }
    })
  })
} else {
  #if DEBUG
  fatalError("Can't hide behind snapshot while presenting other view controller")
  #endif
}

}