CPDFPopMenu.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // CPDFPopMenu.swift
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. public protocol CPDFPopMenuDelegate: AnyObject {
  14. func menuDidClosed(in menu: CPDFPopMenu, isClosed: Bool)
  15. }
  16. public class CPDFPopMenu: UIView {
  17. var backgroundContainer: UIImageView?
  18. var coverLayer: UIButton?
  19. var contentView: UIView?
  20. var dimCoverLayer: Bool = false
  21. public weak var delegate: CPDFPopMenuDelegate?
  22. private var lastRect:CGRect = CGRect.zero
  23. init(contentView: UIView) {
  24. super.init(frame: .zero)
  25. self.contentView = contentView
  26. setUp()
  27. }
  28. required init?(coder aDecoder: NSCoder) {
  29. super.init(coder: aDecoder)
  30. setUp()
  31. }
  32. private func setUp() {
  33. coverLayer = UIButton(type: .custom)
  34. self.setDimCoverLayer(true)
  35. coverLayer?.addTarget(self, action: #selector(coverLayerClicked), for: .touchUpInside)
  36. if coverLayer != nil {
  37. addSubview(coverLayer!)
  38. }
  39. backgroundContainer = UIImageView()
  40. backgroundContainer?.isUserInteractionEnabled = true
  41. if backgroundContainer != nil {
  42. addSubview(backgroundContainer!)
  43. }
  44. }
  45. static func popMenu(with contentView: UIView) -> CPDFPopMenu {
  46. return CPDFPopMenu(contentView: contentView)
  47. }
  48. // MARK: - Usage
  49. @objc public func showMenu(in rect: CGRect) {
  50. self.lastRect = rect
  51. guard let window = UIApplication.shared.keyWindow else { return }
  52. frame = window.bounds
  53. window.addSubview(self)
  54. coverLayer?.frame = window.bounds
  55. backgroundContainer?.frame = rect
  56. if let contentView = contentView {
  57. let topMargin: CGFloat = 12
  58. let leftMargin: CGFloat = 5
  59. let bottomMargin: CGFloat = 8
  60. let rightMargin: CGFloat = 5
  61. contentView.frame.origin.x = leftMargin
  62. contentView.frame.origin.y = topMargin
  63. contentView.frame.size.width = (backgroundContainer?.frame.width ?? 0) - leftMargin - rightMargin
  64. contentView.frame.size.height = (backgroundContainer?.frame.height ?? 0) - topMargin - bottomMargin
  65. backgroundContainer?.addSubview(contentView)
  66. }
  67. delegate?.menuDidClosed(in: self, isClosed: false)
  68. }
  69. @objc func coverLayerClicked() {
  70. hideMenu()
  71. }
  72. public override func layoutSubviews() {
  73. super.layoutSubviews()
  74. }
  75. func hideMenu() {
  76. removeFromSuperview()
  77. if delegate?.menuDidClosed(in: self, isClosed: true) != nil {
  78. delegate?.menuDidClosed(in: self, isClosed: true)
  79. }
  80. }
  81. // MARK: - Property
  82. func setDimCoverLayer(_ dimCoverLayer: Bool) {
  83. if dimCoverLayer {
  84. coverLayer?.backgroundColor = UIColor.black.withAlphaComponent(0.2)
  85. } else {
  86. coverLayer?.backgroundColor = UIColor.clear
  87. }
  88. }
  89. }