O2MindMapCanvasBottomBar.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // O2MindMapCanvasBottomBar.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2021/12/30.
  6. // Copyright © 2021 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. import RxCocoa
  11. enum O2MindMapCanvasBottomBtnType: String {
  12. case createSubNode = "下级" // 创建下级
  13. case createSameLevelNode = "同级" // 创建同级
  14. case editNode = "编辑" // 编辑
  15. case deleteNode = "删除" // 删除
  16. case addIcon = "图标" // 添加图标 进度图标 优先级图标
  17. case addImg = "图片" // 添加图片
  18. case addLink = "超链接" // 添加超链接
  19. }
  20. protocol O2MindMapCanvasBottomBtnDelegate {
  21. func clickBtn(type: O2MindMapCanvasBottomBtnType)
  22. }
  23. /// 脑图画布 底部工具栏
  24. class O2MindMapCanvasBottomBar: UIView {
  25. class func newBar(y: CGFloat) -> O2MindMapCanvasBottomBar {
  26. let bar = O2MindMapCanvasBottomBar()
  27. bar.frame = CGRect(x: 0, y: SCREEN_HEIGHT, width: SCREEN_WIDTH, height: O2MindMapCanvasBottomBar.bottomBarHeight)
  28. bar.backgroundColor = .clear
  29. return bar
  30. }
  31. static let bottomBarHeight:CGFloat = 48
  32. private let btnWidth:CGFloat = 90
  33. private let btnGap:CGFloat = 8
  34. private let btnHeight:CGFloat = 36
  35. private var myScollView: UIScrollView? = nil
  36. private var isRoot = false
  37. private let rootBtns: [O2MindMapCanvasBottomBtnType] = [.createSubNode, .editNode , .addImg, .addIcon, .addLink]
  38. private let nodeBtns: [O2MindMapCanvasBottomBtnType] = [.createSubNode, .createSameLevelNode, .editNode, .deleteNode, .addImg, .addIcon, .addLink]
  39. var delegate: O2MindMapCanvasBottomBtnDelegate? = nil
  40. func show(isRoot: Bool) {
  41. self.isRoot = isRoot
  42. self.loadBtns()
  43. UIView.animate(withDuration: 0.3) {
  44. self.frame.origin.y = SCREEN_HEIGHT - safeAreaTopHeight - 10 - O2MindMapCanvasBottomBar.bottomBarHeight
  45. } completion: { finish in
  46. DDLogDebug("show is finish。。。。。。。。。。\(finish)")
  47. }
  48. }
  49. func hide() {
  50. UIView.animate(withDuration: 0.3) {
  51. self.frame.origin.y = SCREEN_HEIGHT
  52. } completion: { finish in
  53. DDLogDebug("hide is finish。。。。。。。。。。\(finish)")
  54. }
  55. }
  56. // 初始化滚动View
  57. func initScollView() {
  58. self.myScollView = UIScrollView()
  59. let y:CGFloat = (O2MindMapCanvasBottomBar.bottomBarHeight - btnHeight) / 2
  60. self.myScollView!.frame = CGRect(x: 0, y: y, width: self.frame.width, height: btnHeight)
  61. self.myScollView!.contentSize = CGSize(width: (btnWidth + btnGap) * 7, height: btnHeight)
  62. self.myScollView?.showsVerticalScrollIndicator = false
  63. self.myScollView?.showsHorizontalScrollIndicator = false
  64. self.myScollView?.alwaysBounceHorizontal = true
  65. self.myScollView?.alwaysBounceVertical = false
  66. self.myScollView?.delegate = self
  67. self.addSubview(self.myScollView!)
  68. }
  69. private func loadBtns() {
  70. if self.myScollView == nil {
  71. self.initScollView()
  72. } else {
  73. self.myScollView?.removeSubviews()
  74. }
  75. if self.isRoot {
  76. self.myScollView!.contentSize = CGSize(width: (btnWidth + btnGap) * CGFloat(self.rootBtns.count), height: btnHeight)
  77. var i = 0
  78. for item in self.rootBtns {
  79. let btn = UIButton()
  80. btn.frame = CGRect(x: CGFloat(btnWidth + btnGap) * CGFloat(i) + btnGap, y: 0, width: btnWidth, height: btnHeight)
  81. btn.setTitle(item.rawValue, for: .normal)
  82. btn.setImage(self.btnIcon(type: item), for: .normal)
  83. btn.setBackgroundColor(.white, forState: .normal)
  84. btn.setTitleColor(.black, for: .normal)
  85. btn.cornerRadius = 4 // roundedCorners(cornerRadius: 4, borderWidth: 1, borderColor: .black)
  86. btn.addShadow(offset: CGSize(width: 5, height: 5), radius: 4, color: .black, opacity: 0.7, cornerRadius: 4)
  87. btn.tag = i
  88. self.myScollView?.addSubview(btn)
  89. btn.addTapGesture { recg in
  90. if let bt = recg.view as? UIButton {
  91. self.clickBtn(tag: bt.tag)
  92. }
  93. }
  94. i += 1
  95. }
  96. } else {
  97. self.myScollView!.contentSize = CGSize(width: (btnWidth + btnGap) * CGFloat(self.nodeBtns.count), height: btnHeight)
  98. var i = 0
  99. for item in self.nodeBtns {
  100. let btn = UIButton()
  101. btn.frame = CGRect(x: CGFloat(btnWidth + btnGap) * CGFloat(i) + btnGap, y: 0, width: btnWidth, height: btnHeight)
  102. btn.setTitle(item.rawValue, for: .normal)
  103. btn.setImage(self.btnIcon(type: item), for: .normal)
  104. btn.setBackgroundColor(.white, forState: .normal)
  105. btn.setTitleColor(.black, for: .normal)
  106. btn.cornerRadius = 4 // roundedCorners(cornerRadius: 4, borderWidth: 1, borderColor: .black)
  107. btn.addShadow(offset: CGSize(width: 5, height: 5), radius: 4, color: .black, opacity: 0.7, cornerRadius: 4)
  108. btn.tag = i
  109. self.myScollView?.addSubview(btn)
  110. btn.addTapGesture { recg in
  111. if let bt = recg.view as? UIButton {
  112. self.clickBtn(tag: bt.tag)
  113. }
  114. }
  115. i += 1
  116. }
  117. }
  118. }
  119. //
  120. private func btnIcon(type: O2MindMapCanvasBottomBtnType)-> UIImage? {
  121. switch type {
  122. case .createSubNode:
  123. return UIImage(named: "icon_xiaji")
  124. case .createSameLevelNode:
  125. return UIImage(named: "icon_tongji")
  126. case .editNode:
  127. return UIImage(named: "icon_bianji_mind")
  128. case .deleteNode:
  129. return UIImage(named: "icon_delete")
  130. case .addIcon:
  131. return UIImage(named: "icon_icon")
  132. case .addImg:
  133. return UIImage(named: "icon_picture")
  134. case .addLink:
  135. return UIImage(named: "icon_chaolianjie")
  136. }
  137. }
  138. private func clickBtn(tag: Int) {
  139. var type: O2MindMapCanvasBottomBtnType? = nil
  140. if self.isRoot {
  141. type = self.rootBtns[tag]
  142. } else {
  143. type = self.nodeBtns[tag]
  144. }
  145. if let type = type {
  146. DDLogDebug("点击了按钮: \(type.rawValue)")
  147. self.delegate?.clickBtn(type: type)
  148. }
  149. }
  150. }
  151. extension O2MindMapCanvasBottomBar: UIScrollViewDelegate {
  152. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  153. }
  154. }