From d036f3ba2d41aec3915aa73ed3936354a5dd2bb4 Mon Sep 17 00:00:00 2001 From: Koji Murata Date: Thu, 27 Jul 2017 01:35:50 +0900 Subject: [PATCH] bug --- .../Classes/PictureInPictureWindow.swift | 37 +++++++++++-------- .../Classes/UIViewController+Extensions.swift | 10 ++++- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/PictureInPicture/Classes/PictureInPictureWindow.swift b/PictureInPicture/Classes/PictureInPictureWindow.swift index 7becfbe..2442326 100644 --- a/PictureInPicture/Classes/PictureInPictureWindow.swift +++ b/PictureInPicture/Classes/PictureInPictureWindow.swift @@ -15,18 +15,25 @@ final class PictureInPictureWindow: UIWindow { private let userInterfaceShutoutView = UIView() func present(with viewController: UIViewController, makeLargerIfNeeded: Bool) { - rootViewController = viewController - - if beforePresenting { - beforePresenting = false - bounds = UIScreen.main.bounds - frame.origin.y = UIScreen.main.bounds.height - UIView.animate(withDuration: animationDuration, delay: 0, options: .curveEaseOut, animations: { - self.frame.origin.y = 0 - }, completion: nil) - makeKeyAndVisible() - } else if makeLargerIfNeeded { - applyLarge() + let handler = { + self.rootViewController = viewController + + if self.beforePresenting { + self.beforePresenting = false + self.bounds = UIScreen.main.bounds + self.frame.origin.y = UIScreen.main.bounds.height + UIView.animate(withDuration: self.animationDuration, delay: 0, options: .curveEaseOut, animations: { + self.frame.origin.y = 0 + }, completion: nil) + self.makeKeyAndVisible() + } else if makeLargerIfNeeded { + self.applyLarge() + } + } + if let rootViewController = rootViewController { + rootViewController.dismissPresentedViewControllers(completion: handler) + } else { + handler() } } @@ -36,7 +43,7 @@ final class PictureInPictureWindow: UIWindow { UIView.animate(withDuration: animationDuration, animations: { self.frame.origin.y = UIScreen.main.bounds.height }, completion: { _ in - self.rootViewController?.dismissWithPresentedViewController { + self.rootViewController?.dismissPresentedViewControllers { self.disposeHandler() NotificationCenter.default.post(name: .PictureInPictureDismissed, object: nil) } @@ -45,14 +52,14 @@ final class PictureInPictureWindow: UIWindow { UIView.animate(withDuration: animationDuration, animations: { self.alpha = 0 }, completion: { _ in - self.rootViewController?.dismissWithPresentedViewController { + self.rootViewController?.dismissPresentedViewControllers { self.disposeHandler() NotificationCenter.default.post(name: .PictureInPictureDismissed, object: nil) } }) } } else { - self.rootViewController?.dismissWithPresentedViewController { + self.rootViewController?.dismissPresentedViewControllers { self.disposeHandler() NotificationCenter.default.post(name: .PictureInPictureDismissed, object: nil) } diff --git a/PictureInPicture/Classes/UIViewController+Extensions.swift b/PictureInPicture/Classes/UIViewController+Extensions.swift index 52d06c9..f57aeff 100644 --- a/PictureInPicture/Classes/UIViewController+Extensions.swift +++ b/PictureInPicture/Classes/UIViewController+Extensions.swift @@ -14,7 +14,15 @@ extension UIViewController { view.setNeedsUpdateConstraints() } - func dismissWithPresentedViewController(completion: @escaping () -> Void) { + func dismissPresentedViewControllers(completion: @escaping () -> Void) { + if let vc = presentedViewController { + vc.dismissWithPresentedViewController(completion: completion) + } else { + completion() + } + } + + private func dismissWithPresentedViewController(completion: @escaping () -> Void) { if let vc = presentedViewController { vc.dismissWithPresentedViewController { self.dismiss(animated: false, completion: completion)