PQSelectedOprationView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // PQSelectedOprationView.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/12/11.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. // MARK: - 选择操作view
  10. /// 选择操作view
  11. public class PQSelectedOprationView: UIView {
  12. public let itemH: CGFloat = cDefaultMargin * 6
  13. public let lineH: CGFloat = 1
  14. public let margin: CGFloat = 8
  15. public var contentH: CGFloat = 0
  16. public var itemList: [String]? {
  17. didSet {
  18. if (itemList?.count ?? 0) > 0 {
  19. contentH = itemH + margin + cSafeAreaHeight + CGFloat(itemList?.count ?? 0) * (itemH + 1)
  20. contentView.frame = CGRect(x: 0, y: cScreenHeigth, width: cScreenWidth, height: contentH)
  21. contentView.addCorner(roundingCorners: [.topLeft, .topRight], corner: 11)
  22. cancelBtn.frame = CGRect(x: 0, y: contentH - itemH - cSafeAreaHeight, width: cScreenWidth, height: itemH + cSafeAreaHeight)
  23. }
  24. }
  25. }
  26. public var completeHander: ((_ sender: UIButton) -> Void)?
  27. lazy public var contentView: UIView = {
  28. let contentView = UIView(frame: CGRect(x: 0, y: cScreenHeigth, width: cScreenWidth, height: itemH + margin))
  29. contentView.backgroundColor = UIColor.black
  30. contentView.addCorner(roundingCorners: [.topLeft, .topRight], corner: 11)
  31. return contentView
  32. }()
  33. lazy public var cancelBtn: UIButton = {
  34. let cancelBtn = UIButton(type: .custom)
  35. cancelBtn.setTitle("取消", for: .normal)
  36. cancelBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#BDBDBD"), for: .normal)
  37. cancelBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#2C2C2C")
  38. cancelBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  39. cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
  40. return cancelBtn
  41. }()
  42. override public init(frame: CGRect) {
  43. super.init(frame: frame)
  44. backgroundColor = cShadowColor
  45. addSubview(contentView)
  46. contentView.addSubview(cancelBtn)
  47. let ges = UITapGestureRecognizer(target: self, action: #selector(removeView))
  48. addGestureRecognizer(ges)
  49. }
  50. required public init?(coder _: NSCoder) {
  51. fatalError("init(coder:) has not been implemented")
  52. }
  53. override public func layoutSubviews() {
  54. super.layoutSubviews()
  55. addSubViews()
  56. }
  57. public func addSubViews() {
  58. if (itemList?.count ?? 0) > 0 {
  59. for (index, item) in itemList!.enumerated() {
  60. let normalBtn = UIButton(type: .custom)
  61. normalBtn.frame = CGRect(x: 0, y: (itemH + 1) * CGFloat(index), width: frame.width, height: itemH)
  62. normalBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#2C2C2C")
  63. normalBtn.setTitle(item, for: .normal)
  64. normalBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#BDBDBD"), for: .normal)
  65. normalBtn.tag = index + 1
  66. normalBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
  67. normalBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  68. contentView.addSubview(normalBtn)
  69. let lineView = UIView(frame: CGRect(x: 0, y: normalBtn.frame.maxY, width: frame.width, height: lineH))
  70. lineView.backgroundColor = UIColor.hexColor(hexadecimal: "#212223")
  71. contentView.addSubview(lineView)
  72. contentH = contentH + (itemH + 1)
  73. }
  74. }
  75. }
  76. @objc public func removeView() {
  77. UIView.animate(withDuration: 0.3, animations: { [weak self] in
  78. self?.contentView.frame = CGRect(x: 0, y: cScreenHeigth, width: cScreenWidth, height: self!.contentH)
  79. }) { [weak self] _ in
  80. self?.removeFromSuperview()
  81. }
  82. }
  83. class public func showSelectedOprationView(itemList: [String], completeHander: @escaping ((_ sender: UIButton) -> Void)) {
  84. let selectedOprationView: PQSelectedOprationView = PQSelectedOprationView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  85. selectedOprationView.completeHander = completeHander
  86. selectedOprationView.itemList = itemList
  87. UIApplication.shared.keyWindow?.addSubview(selectedOprationView)
  88. let contentH: CGFloat = 68.0 + cSafeAreaHeight + CGFloat(itemList.count) * 61.0
  89. UIView.animate(withDuration: 0.3, animations: {
  90. selectedOprationView.contentView.frame = CGRect(x: 0, y: cScreenHeigth - contentH, width: cScreenWidth, height: contentH)
  91. }) { _ in
  92. }
  93. }
  94. @objc public func btnClick(sender: UIButton) {
  95. if completeHander != nil {
  96. completeHander!(sender)
  97. }
  98. removeView()
  99. }
  100. }