From 0b74b4238f3d6ce4e331e5634984f90ee37e8b02 Mon Sep 17 00:00:00 2001 From: PP Date: Fri, 8 Jul 2022 10:36:41 +0900 Subject: [PATCH] Adding center display location --- Sources/Loaf/Loaf.swift | 4 +++- Sources/Loaf/Presenter/Animator.swift | 9 ++++++++- Sources/Loaf/Presenter/Controller.swift | 4 ++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Sources/Loaf/Loaf.swift b/Sources/Loaf/Loaf.swift index c256f1a..5a4a5ec 100644 --- a/Sources/Loaf/Loaf.swift +++ b/Sources/Loaf/Loaf.swift @@ -95,16 +95,18 @@ final public class Loaf { /// /// - top: Top of the display /// - bottom: Bottom of the display + /// - center: Center of the display public enum Location { case top case bottom + case center } /// Defines either the presenting or dismissing direction of loaf. (Default is `.vertical`) /// /// - left: To / from the left /// - right: To / from the right - /// - vertical: To / from the top or bottom (depending on the location of the loaf) + /// - vertical: To / from the top, center or bottom (depending on the location of the loaf) public enum Direction { case left case right diff --git a/Sources/Loaf/Presenter/Animator.swift b/Sources/Loaf/Presenter/Animator.swift index 4d15f4a..3d22327 100644 --- a/Sources/Loaf/Presenter/Animator.swift +++ b/Sources/Loaf/Presenter/Animator.swift @@ -40,7 +40,14 @@ extension Animator: UIViewControllerAnimatedTransitioning { switch presenting ? loaf.presentingDirection : loaf.dismissingDirection { case .vertical: - dismissedFrame.origin.y = (loaf.location == .bottom) ? controller.view.frame.height + 60 : -size.height - 60 + switch loaf.location { + case .top: + dismissedFrame.origin.y = -size.height - 60 + case .bottom: + dismissedFrame.origin.y = controller.view.bounds.height + 60 + case .center: + dismissedFrame.origin.y = 60 + } case .left: dismissedFrame.origin.x = -controller.view.frame.width * 2 case .right: diff --git a/Sources/Loaf/Presenter/Controller.swift b/Sources/Loaf/Presenter/Controller.swift index 8ed470a..d1f5874 100644 --- a/Sources/Loaf/Presenter/Controller.swift +++ b/Sources/Loaf/Presenter/Controller.swift @@ -57,6 +57,8 @@ final class Controller: UIPresentationController { yPosition = containerView.frame.origin.y + containerView.frame.height - size.height - containerInsets.bottom case .top: yPosition = containerInsets.top + case .center: + yPosition = containerView.frame.midY - (size.height/2.0) } containerView.frame.origin = CGPoint( @@ -81,6 +83,8 @@ final class Controller: UIPresentationController { yPosition = containerView.bounds.height - containerSize.height case .top: yPosition = 0 + case .center: + yPosition = containerView.bounds.midY - (containerSize.height/2.0) } let toastSize = CGRect(x: containerView.center.x - (containerSize.width / 2),