From 6b1edd1dfbd5e70fbd30d6a029791b4ce7d521c4 Mon Sep 17 00:00:00 2001 From: Nikita Nikitsky Date: Wed, 2 Oct 2019 02:53:39 +0400 Subject: [PATCH] Added the ability to select a color for the background of the pan modal container (#54) --- .../Controller/PanModalPresentationController.swift | 4 ++-- .../Presentable/PanModalPresentable+Defaults.swift | 4 ++-- PanModal/Presentable/PanModalPresentable.swift | 8 ++++---- PanModal/View/DimmedView.swift | 12 ++++-------- .../TransientAlertViewController.swift | 4 ++-- .../View Controllers/Alert/AlertViewController.swift | 4 ++-- Tests/PanModalTests.swift | 2 +- 7 files changed, 17 insertions(+), 21 deletions(-) diff --git a/PanModal/Controller/PanModalPresentationController.swift b/PanModal/Controller/PanModalPresentationController.swift index 1f98403..e497a28 100644 --- a/PanModal/Controller/PanModalPresentationController.swift +++ b/PanModal/Controller/PanModalPresentationController.swift @@ -105,8 +105,8 @@ public class PanModalPresentationController: UIPresentationController { */ private lazy var backgroundView: DimmedView = { let view: DimmedView - if let alpha = presentable?.backgroundAlpha { - view = DimmedView(dimAlpha: alpha) + if let color = presentable?.panModalBackgroundColor { + view = DimmedView(dimColor: color) } else { view = DimmedView() } diff --git a/PanModal/Presentable/PanModalPresentable+Defaults.swift b/PanModal/Presentable/PanModalPresentable+Defaults.swift index a7ae976..a69fc53 100644 --- a/PanModal/Presentable/PanModalPresentable+Defaults.swift +++ b/PanModal/Presentable/PanModalPresentable+Defaults.swift @@ -46,8 +46,8 @@ public extension PanModalPresentable where Self: UIViewController { return [.curveEaseInOut, .allowUserInteraction, .beginFromCurrentState] } - var backgroundAlpha: CGFloat { - return 0.7 + var panModalBackgroundColor: UIColor { + return UIColor.black.withAlphaComponent(0.7) } var scrollIndicatorInsets: UIEdgeInsets { diff --git a/PanModal/Presentable/PanModalPresentable.swift b/PanModal/Presentable/PanModalPresentable.swift index e24baa3..a5aab6b 100644 --- a/PanModal/Presentable/PanModalPresentable.swift +++ b/PanModal/Presentable/PanModalPresentable.swift @@ -86,13 +86,13 @@ public protocol PanModalPresentable: AnyObject { var transitionAnimationOptions: UIView.AnimationOptions { get } /** - The background view alpha. + The background view color. - Note: This is only utilized at the very start of the transition. - Default Value is 0.7. - */ - var backgroundAlpha: CGFloat { get } + Default Value is black with alpha component 0.7. + */ + var panModalBackgroundColor: UIColor { get } /** We configure the panScrollable's scrollIndicatorInsets interally so override this value diff --git a/PanModal/View/DimmedView.swift b/PanModal/View/DimmedView.swift index 2ee8ada..dea2637 100644 --- a/PanModal/View/DimmedView.swift +++ b/PanModal/View/DimmedView.swift @@ -31,12 +31,11 @@ public class DimmedView: UIView { didSet { switch dimState { case .max: - alpha = dimAlpha + alpha = 1.0 case .off: alpha = 0.0 case .percent(let percentage): - let val = max(0.0, min(1.0, percentage)) - alpha = dimAlpha * val + alpha = max(0.0, min(1.0, percentage)) } } } @@ -53,15 +52,12 @@ public class DimmedView: UIView { return UITapGestureRecognizer(target: self, action: #selector(didTapView)) }() - private let dimAlpha: CGFloat - // MARK: - Initializers - init(dimAlpha: CGFloat = 0.7) { - self.dimAlpha = dimAlpha + init(dimColor: UIColor = UIColor.black.withAlphaComponent(0.7)) { super.init(frame: .zero) alpha = 0.0 - backgroundColor = .black + backgroundColor = dimColor addGestureRecognizer(tapGesture) } diff --git a/Sample/View Controllers/Alert (Transient)/TransientAlertViewController.swift b/Sample/View Controllers/Alert (Transient)/TransientAlertViewController.swift index ffba0de..ee9b669 100644 --- a/Sample/View Controllers/Alert (Transient)/TransientAlertViewController.swift +++ b/Sample/View Controllers/Alert (Transient)/TransientAlertViewController.swift @@ -59,8 +59,8 @@ class TransientAlertViewController: AlertViewController { return true } - override var backgroundAlpha: CGFloat { - return 0.0 + override var panModalBackgroundColor: UIColor { + return .clear } override var isUserInteractionEnabled: Bool { diff --git a/Sample/View Controllers/Alert/AlertViewController.swift b/Sample/View Controllers/Alert/AlertViewController.swift index 60715a3..951e3e2 100644 --- a/Sample/View Controllers/Alert/AlertViewController.swift +++ b/Sample/View Controllers/Alert/AlertViewController.swift @@ -46,8 +46,8 @@ class AlertViewController: UIViewController, PanModalPresentable { return shortFormHeight } - var backgroundAlpha: CGFloat { - return 0.1 + var panModalBackgroundColor: UIColor { + return UIColor.black.withAlphaComponent(0.1) } var shouldRoundTopCorners: Bool { diff --git a/Tests/PanModalTests.swift b/Tests/PanModalTests.swift index a2b3969..f5d499b 100644 --- a/Tests/PanModalTests.swift +++ b/Tests/PanModalTests.swift @@ -48,7 +48,7 @@ class PanModalTests: XCTestCase { XCTAssertEqual(vc.shortFormHeight, PanModalHeight.maxHeight) XCTAssertEqual(vc.longFormHeight, PanModalHeight.maxHeight) XCTAssertEqual(vc.springDamping, 0.8) - XCTAssertEqual(vc.backgroundAlpha, 0.7) + XCTAssertEqual(vc.panModalBackgroundColor, UIColor.black.withAlphaComponent(0.7)) XCTAssertEqual(vc.scrollIndicatorInsets, .zero) XCTAssertEqual(vc.anchorModalToLongForm, true) XCTAssertEqual(vc.allowsExtendedPanScrolling, false)