From d51db6f3b9cc7330cb445b365328cbfae17b20ab Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Mon, 16 Oct 2017 17:36:46 -0400 Subject: [PATCH] Remove toast on pan completion and avoid recenter exception (#592) --- Classes/Systems/Toast.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Classes/Systems/Toast.swift b/Classes/Systems/Toast.swift index d377e09a..a825c045 100644 --- a/Classes/Systems/Toast.swift +++ b/Classes/Systems/Toast.swift @@ -250,6 +250,7 @@ final class ToastManager { } func dismiss() { + invalidateTimer() animator.removeBehavior(springBehavior) UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseInOut], animations: { @@ -261,6 +262,9 @@ final class ToastManager { } func recenter() { + // UIDynamics will throw if adding the behavior back when the view has already been removed + guard view.superview != nil else { return } + animator.removeBehavior(springBehavior) springBehavior.anchorPoint = anchor(view: view, viewController: viewController) animator.addBehavior(springBehavior) @@ -347,7 +351,9 @@ final class ToastManager { var center = toast.view.center center.y = finalY toast.view.center = center - }) + }) { _ in + toast.view.removeFromSuperview() + } } else { toast.startTimer() toast.animator.addBehavior(toast.springBehavior)