O2WorkMoreActionSheet.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // O2WorkMoreActionSheet.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/5/22.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. private let keyWindow = UIApplication.shared.keyWindow
  11. class O2WorkMoreActionSheet: UIView, UIGestureRecognizerDelegate {
  12. typealias DidTapButton = (_ item: WorkNewActionItem) -> Void
  13. ////TODO 这些高度还没有适配全面屏。。。
  14. private let buttonHeight: CGFloat = 44.0
  15. private let toolbarHeight: CGFloat = 44.0
  16. private let buttonGap: CGFloat = 1.0
  17. private let bottomGap: CGFloat = 5.0
  18. private var toolBar: UIToolbar!
  19. private var didTap: DidTapButton?
  20. required init?(coder aDecoder: NSCoder) {
  21. fatalError("init(coder:) has not been implemented")
  22. }
  23. init(moreControls: [WorkNewActionItem], didTapButton: @escaping DidTapButton) {
  24. super.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
  25. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(close))
  26. tapGesture.delegate = self
  27. addGestureRecognizer(tapGesture)
  28. backgroundColor = .clear
  29. self.didTap = didTapButton
  30. //toolbar
  31. self.toolBar = UIToolbar(frame: CGRect(x: 0, y: self.frame.height - self.buttonHeight, width: self.frame.width, height: self.toolbarHeight))
  32. self.toolBar.items = self.toolBarItems(moreControls: moreControls)
  33. self.addSubview(self.toolBar)
  34. moreControls.forEachEnumerated { (index, item) in
  35. if index > 1 {
  36. let moreIndex = CGFloat.init(index - 2)
  37. let y = ( moreIndex * self.buttonHeight + moreIndex * self.buttonGap + self.buttonGap + self.toolbarHeight )
  38. DDLogDebug("button:\(y)")
  39. self.generateButton(y: y, item: item)
  40. }
  41. }
  42. }
  43. //打开
  44. open func show() {
  45. keyWindow?.addSubview(self)
  46. keyWindow?.bringSubviewToFront(self)
  47. self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
  48. }
  49. private func toolBarItems(moreControls: [WorkNewActionItem]) -> [UIBarButtonItem] {
  50. let firstItem = moreControls[0]
  51. let secondItem = moreControls[1]
  52. var items: [UIBarButtonItem] = []
  53. let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
  54. let firstButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  55. firstButton.setTitle(firstItem.text, for: .normal)
  56. firstButton.setTitleColor(base_color, for: .normal)
  57. firstButton.addTapGesture { (tap) in
  58. self.clickButton(item: firstItem)
  59. }
  60. let firstButtonItem = UIBarButtonItem(customView: firstButton)
  61. items.append(spaceItem)
  62. items.append(firstButtonItem)
  63. items.append(spaceItem)
  64. let secondButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  65. secondButton.setTitle(secondItem.text, for: .normal)
  66. secondButton.setTitleColor(base_color, for: .normal)
  67. secondButton.addTapGesture { (tap) in
  68. self.clickButton(item: secondItem)
  69. }
  70. let secondButtonItem = UIBarButtonItem(customView: secondButton)
  71. items.append(spaceItem)
  72. items.append(secondButtonItem)
  73. items.append(spaceItem)
  74. let moreButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  75. moreButton.setImage(UIImage(named: "icon_more_s"), for: .normal)
  76. moreButton.addTapGesture { (tap) in
  77. self.close()
  78. }
  79. let moreButtonItem = UIBarButtonItem(customView: moreButton)
  80. items.append(moreButtonItem)
  81. return items
  82. }
  83. //生成按钮
  84. private func generateButton(y: CGFloat, item:WorkNewActionItem) {
  85. let button = UIButton(type: UIButton.ButtonType.system)
  86. let buttonY = self.frame.height - y - self.buttonHeight
  87. DDLogDebug("button y:\(buttonY)")
  88. button.frame = CGRect(x: CGFloat(0.0), y: buttonY, width: CGFloat(self.frame.width), height: CGFloat(self.buttonHeight))
  89. button.setTitle(item.text, for: .normal)
  90. button.setTitleColor(base_color, for: .normal)
  91. button.backgroundColor = UIColor.white
  92. button.addTapGesture { (tap) in
  93. DDLogDebug("点击了 \(item.text) , action: \(item.action) , actionScript:\(item.actionScript)")
  94. self.clickButton(item: item)
  95. }
  96. self.addSubview(button)
  97. }
  98. private func clickButton(item: WorkNewActionItem) {
  99. self.didTap?(item)
  100. self.close()
  101. }
  102. //关闭
  103. @objc private func close() {
  104. self.backgroundColor = .clear
  105. self.removeFromSuperview()
  106. }
  107. }