DropUpMenu.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // DropUpMenu.swift
  3. // DropUpMenu
  4. //
  5. // Created by Suric on 16/8/11.
  6. // Copyright © 2016年 teambition. All rights reserved.
  7. //
  8. import UIKit
  9. public protocol DropUpMenuDelegate: class {
  10. func dropUpMenu(_ dropUpMenu: DropUpMenu, cellForRowAt indexPath: IndexPath) -> UITableViewCell?
  11. func dropUpMenu(_ dropUpMenu: DropUpMenu, didSelectRowAt indexPath: IndexPath)
  12. func dropUpMenuCancel(_ dropUpMenu: DropUpMenu)
  13. func dropUpMenuWillDismiss(_ dropUpMenu: DropUpMenu)
  14. func dropUpMenuWillShow(_ dropUpMenu: DropUpMenu)
  15. }
  16. public extension DropUpMenuDelegate {
  17. func dropUpMenu(_ dropUpMenu: DropUpMenu, cellForRowAt indexPath: IndexPath) -> UITableViewCell? {
  18. return nil
  19. }
  20. func dropUpMenu(_ dropUpMenu: DropUpMenu, didSelectRowAt indexPath: IndexPath) { }
  21. func dropUpMenuCancel(_ dropUpMenu: DropUpMenu) { }
  22. func dropUpMenuWillDismiss(_ dropUpMenu: DropUpMenu) { }
  23. func dropUpMenuWillShow(_ dropUpMenu: DropUpMenu) { }
  24. }
  25. private let screenRect = UIScreen.main.bounds
  26. open class DropUpMenu: UIView {
  27. fileprivate var items: [DropdownItem] = []
  28. fileprivate var selectedRow: Int
  29. open var tableView: UITableView!
  30. fileprivate var barCoverView: UIView!
  31. fileprivate var isShow = false
  32. fileprivate var addedWindow: UIWindow?
  33. fileprivate var windowRootView: UIView?
  34. fileprivate lazy var tapGestureRecognizer: UITapGestureRecognizer = {
  35. return UITapGestureRecognizer(target: self, action: #selector(self.hideMenu))
  36. }()
  37. open weak var delegate: DropUpMenuDelegate?
  38. open var animateDuration: TimeInterval = 0.25
  39. open var backgroudBeginColor: UIColor = UIColor.black.withAlphaComponent(0)
  40. open var backgroudEndColor = UIColor(white: 0, alpha: 0.4)
  41. open var rowHeight: CGFloat = 50
  42. open var tableViewHeight: CGFloat = 0
  43. open var defaultBottonMargin: CGFloat = 150
  44. open var textColor: UIColor = UIColor(red: 56.0/255.0, green: 56.0/255.0, blue: 56.0/255.0, alpha: 1.0)
  45. open var highlightColor: UIColor = UIColor(red: 3.0/255.0, green: 169.0/255.0, blue: 244.0/255.0, alpha: 1.0)
  46. open var tableViewBackgroundColor: UIColor = UIColor(red: 242.0/255.0, green: 242.0/255.0, blue: 242.0/255.0, alpha: 1.0) {
  47. didSet {
  48. tableView.backgroundColor = tableViewBackgroundColor
  49. }
  50. }
  51. open var tableViewSeperatorColor = UIColor(red: 217.0/255.0, green: 217.0/255.0, blue: 217.0/255.0, alpha: 1.0) {
  52. didSet {
  53. tableView.separatorColor = tableViewSeperatorColor
  54. }
  55. }
  56. open var cellBackgroundColor = UIColor.white
  57. open var displaySelected: Bool = true
  58. open var bottomOffsetY: CGFloat = 0
  59. required public init?(coder aDecoder: NSCoder) {
  60. fatalError("init(coder:) has not been implemented")
  61. }
  62. public init(items: [DropdownItem], selectedRow: Int = 0, bottomOffsetY: CGFloat = 0) {
  63. self.items = items
  64. self.selectedRow = selectedRow
  65. self.bottomOffsetY = bottomOffsetY
  66. let frame = CGRect(x: 0, y: 0, width: screenRect.width, height: screenRect.height - bottomOffsetY)
  67. super.init(frame: frame)
  68. clipsToBounds = true
  69. setupGestureView()
  70. initTableView()
  71. }
  72. fileprivate func setupGestureView() {
  73. let gestureView = UIView()
  74. gestureView.backgroundColor = UIColor.clear
  75. addSubview(gestureView)
  76. gestureView.translatesAutoresizingMaskIntoConstraints = false
  77. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: gestureView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0)])
  78. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: gestureView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0)])
  79. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: gestureView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
  80. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: gestureView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
  81. gestureView.addGestureRecognizer(tapGestureRecognizer)
  82. }
  83. fileprivate func initTableView() {
  84. tableView = UITableView(frame: CGRect.zero, style: .grouped)
  85. tableView?.delegate = self
  86. tableView?.dataSource = self
  87. tableView.estimatedSectionHeaderHeight = 0
  88. tableView.estimatedSectionFooterHeight = 0
  89. addSubview(tableView)
  90. }
  91. fileprivate func layoutTableView() {
  92. tableViewHeight = CGFloat(items.count) * rowHeight
  93. let maxHeight = UIScreen.main.bounds.height - bottomOffsetY
  94. if tableViewHeight > maxHeight {
  95. tableViewHeight = maxHeight
  96. }
  97. tableView.translatesAutoresizingMaskIntoConstraints = false
  98. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView as Any, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant:0)])
  99. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView as Any, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: tableViewHeight)])
  100. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView as Any, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
  101. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView as Any, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
  102. }
  103. fileprivate func setupBottomSeperatorView() {
  104. let seperatorView = UIView()
  105. seperatorView.backgroundColor = tableViewSeperatorColor
  106. addSubview(seperatorView)
  107. seperatorView.translatesAutoresizingMaskIntoConstraints = false
  108. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0)])
  109. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
  110. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
  111. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0.5)])
  112. }
  113. fileprivate func setupBottomCoverView(on view: UIView) {
  114. barCoverView = UIView()
  115. barCoverView.backgroundColor = UIColor.clear
  116. barCoverView.translatesAutoresizingMaskIntoConstraints = false
  117. view.addSubview(barCoverView)
  118. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: barCoverView as Any, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0)])
  119. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: barCoverView as Any, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: bottomOffsetY)])
  120. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: barCoverView as Any, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 0)])
  121. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: barCoverView as Any, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1.0, constant: 0)])
  122. barCoverView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideMenu)))
  123. }
  124. open func showMenu() {
  125. delegate?.dropUpMenuWillShow(self)
  126. if isShow {
  127. hideMenu()
  128. return
  129. }
  130. isShow = true
  131. layoutTableView()
  132. setupBottomSeperatorView()
  133. if let rootView = UIApplication.shared.keyWindow {
  134. windowRootView = rootView
  135. } else {
  136. addedWindow = UIWindow(frame: UIScreen.main.bounds)
  137. addedWindow?.rootViewController = UIViewController()
  138. addedWindow?.isHidden = false
  139. addedWindow?.makeKeyAndVisible()
  140. windowRootView = addedWindow!
  141. }
  142. setupBottomCoverView(on: windowRootView!)
  143. windowRootView?.addSubview(self)
  144. translatesAutoresizingMaskIntoConstraints = false
  145. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: self, attribute: .top, relatedBy: .equal, toItem: windowRootView, attribute: .top, multiplier: 1.0, constant: 0)])
  146. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: self, attribute: .bottom, relatedBy: .equal, toItem: windowRootView, attribute: .bottom, multiplier: 1.0, constant: -bottomOffsetY)])
  147. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: self, attribute: .left, relatedBy: .equal, toItem: windowRootView, attribute: .left, multiplier: 1.0, constant: 0)])
  148. NSLayoutConstraint.activate([NSLayoutConstraint.init(item: self, attribute: .right, relatedBy: .equal, toItem: windowRootView, attribute: .right, multiplier: 1.0, constant: 0)])
  149. backgroundColor = backgroudBeginColor
  150. self.tableView.frame.origin.y = screenRect.height - bottomOffsetY
  151. UIView.animate(withDuration: animateDuration, delay: 0, options: UIView.AnimationOptions(rawValue: 7<<16), animations: {
  152. self.backgroundColor = self.backgroudEndColor
  153. self.tableView.frame.origin.y = screenRect.height - self.bottomOffsetY - self.tableViewHeight
  154. }, completion: nil)
  155. }
  156. @objc open func hideMenu(isSelectAction: Bool = false) {
  157. delegate?.dropUpMenuWillDismiss(self)
  158. UIView.animate(withDuration: animateDuration, animations: {
  159. self.backgroundColor = self.backgroudBeginColor
  160. self.tableView.frame.origin.y = screenRect.height - self.bottomOffsetY
  161. }, completion: { (finished) in
  162. if !isSelectAction {
  163. self.delegate?.dropUpMenuCancel(self)
  164. }
  165. self.barCoverView.removeFromSuperview()
  166. self.removeFromSuperview()
  167. self.isShow = false
  168. if let _ = self.addedWindow {
  169. self.addedWindow?.isHidden = true
  170. UIApplication.shared.keyWindow?.makeKey()
  171. }
  172. })
  173. }
  174. }
  175. extension DropUpMenu: UITableViewDataSource {
  176. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  177. return items.count
  178. }
  179. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  180. if let customCell = delegate?.dropUpMenu(self, cellForRowAt: indexPath) {
  181. return customCell
  182. }
  183. let item = items[(indexPath as NSIndexPath).row]
  184. let cell = UITableViewCell(style: .default, reuseIdentifier: "dropUpMenuCell")
  185. switch item.style {
  186. case .default:
  187. cell.textLabel?.textColor = textColor
  188. if let image = item.image {
  189. cell.imageView?.image = image
  190. }
  191. case .highlight:
  192. cell.textLabel?.textColor = highlightColor
  193. if let image = item.image {
  194. let highlightImage = image.withRenderingMode(.alwaysTemplate)
  195. cell.imageView?.image = highlightImage
  196. cell.imageView?.tintColor = highlightColor
  197. }
  198. }
  199. cell.textLabel?.text = item.title
  200. cell.tintColor = highlightColor
  201. cell.backgroundColor = cellBackgroundColor
  202. if displaySelected && (indexPath as NSIndexPath).row == selectedRow {
  203. cell.accessoryType = .checkmark
  204. } else {
  205. cell.accessoryType = .none
  206. }
  207. if let accesoryImage = item.accessoryImage {
  208. cell.accessoryView = UIImageView(image: accesoryImage)
  209. }
  210. return cell
  211. }
  212. }
  213. extension DropUpMenu: UITableViewDelegate {
  214. public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  215. return rowHeight
  216. }
  217. public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  218. return CGFloat.leastNonzeroMagnitude
  219. }
  220. public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  221. return CGFloat.leastNormalMagnitude
  222. }
  223. public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  224. if displaySelected {
  225. let item = items[(indexPath as NSIndexPath).row]
  226. if item.accessoryImage == nil {
  227. let previousSelectedcell = tableView.cellForRow(at: IndexPath(row: selectedRow, section: 0))
  228. previousSelectedcell?.accessoryType = .none
  229. selectedRow = (indexPath as NSIndexPath).row
  230. let cell = tableView.cellForRow(at: indexPath)
  231. cell?.accessoryType = .checkmark
  232. }
  233. }
  234. tableView.deselectRow(at: indexPath, animated: true)
  235. hideMenu(isSelectAction: true)
  236. delegate?.dropUpMenu(self, didSelectRowAt: indexPath)
  237. }
  238. }