KMQucikToolsView.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // KMQucikToolsView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/10/27.
  6. //
  7. import Cocoa
  8. typealias KMQucikToolsViewDidSelect = (_ view: KMQucikToolsView, _ item: KMQucikToolsModel) -> Void
  9. typealias KMQucikToolsViewPageChange = (_ view: KMQucikToolsView) -> Void
  10. class KMQucikToolsView: KMBaseXibView {
  11. @IBOutlet weak var collectionView: NSCollectionView!
  12. var didSelect: KMQucikToolsViewDidSelect?
  13. var pageChange: KMQucikToolsViewPageChange?
  14. var data: [KMQucikToolsModel] = []
  15. override func draw(_ dirtyRect: NSRect) {
  16. super.draw(dirtyRect)
  17. // Drawing code here.
  18. }
  19. override func setup() {
  20. //设置代理
  21. let layout = NSCollectionViewFlowLayout()
  22. layout.scrollDirection = .horizontal
  23. layout.minimumLineSpacing = 10
  24. layout.minimumInteritemSpacing = 10
  25. // 设置布局到 NSCollectionView
  26. self.collectionView.collectionViewLayout = layout
  27. self.collectionView.delegate = self
  28. self.collectionView.dataSource = self
  29. //是否可选中
  30. self.collectionView.isSelectable = true
  31. //注册cell
  32. self.collectionView.register(KMQucikToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem"))
  33. NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll(notification:)), name: NSScrollView.didLiveScrollNotification, object: collectionView.enclosingScrollView)
  34. // self.backgroundColor(KMAppearance.Layout.l0Color())
  35. }
  36. override func reloadData() {
  37. self.data.removeAll()
  38. for type in KMQucikToolsModel.showType() {
  39. self.data.append(KMQucikToolsModel.init(type: type))
  40. }
  41. self.collectionView.reloadData()
  42. }
  43. }
  44. //Notification
  45. extension KMQucikToolsView {
  46. @objc func scrollViewDidScroll(notification: Notification) {
  47. // 处理滚动事件
  48. if let scrollView = notification.object as? NSScrollView {
  49. print("NSScrollView did scroll.")
  50. // 获取滚动位置等信息
  51. let contentOffset = scrollView.contentView.bounds.origin
  52. print("Content Offset: \(contentOffset)")
  53. }
  54. guard let callBack = pageChange else { return }
  55. callBack(self)
  56. }
  57. }
  58. extension KMQucikToolsView: NSCollectionViewDelegate {
  59. //当item被选中
  60. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  61. print("点击")
  62. let view = collectionView.item(at: indexPaths.first!) as! KMQucikToolCollectionViewItem
  63. let content = view.model
  64. guard let callBack = didSelect else { return }
  65. callBack(self, content!)
  66. }
  67. //当item取消选中
  68. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  69. _ = collectionView.item(at: indexPaths.first!) as! KMQucikToolCollectionViewItem
  70. }
  71. }
  72. extension KMQucikToolsView: NSCollectionViewDataSource {
  73. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  74. return 1
  75. }
  76. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  77. return self.data.count
  78. }
  79. //返回对应的item自定义个体
  80. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  81. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem"), for: indexPath) as! KMQucikToolCollectionViewItem
  82. if self.data.count > indexPath.item {
  83. view.model = self.data[indexPath.item]
  84. }
  85. return view
  86. }
  87. }
  88. extension KMQucikToolsView: NSCollectionViewDelegateFlowLayout {
  89. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  90. return NSSize(width: 216, height: 96)
  91. }
  92. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  93. return NSEdgeInsets(top: 10, left: 0, bottom: 10, right: 10)
  94. }
  95. }
  96. //Collection Page
  97. extension KMQucikToolsView {
  98. func pageCount() -> Int {
  99. return Int(ceilf(Float(self.collectionView.frame.size.width / self.collectionView.visibleRect.size.width)))
  100. }
  101. func currentPage() -> Int {
  102. return Int(ceilf(Float(self.collectionView.visibleRect.origin.x / self.collectionView.visibleRect.size.width))) + 1
  103. }
  104. func nextPage() {
  105. let currentPage = self.currentPage()
  106. let pageCount = self.pageCount()
  107. if currentPage < pageCount {
  108. self.collectionView.scroll(CGPoint(x: Int(self.collectionView.visibleRect.size.width) * currentPage, y: 0))
  109. guard let callBack = pageChange else { return }
  110. callBack(self)
  111. }
  112. }
  113. func previousPage() {
  114. let currentPage = self.currentPage()
  115. let pageCount = self.pageCount()
  116. if currentPage > 1 {
  117. self.collectionView.scroll(CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (currentPage - 2), y: 0))
  118. guard let callBack = pageChange else { return }
  119. callBack(self)
  120. }
  121. }
  122. }