KMNCustomCollectionViewLayout.swift 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //
  2. // KMNCustomCollectionViewLayout.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/11/9.
  6. //
  7. import Cocoa
  8. class KMNThumCustomCollectionViewFlowLayout: NSCollectionViewFlowLayout {
  9. var lineAttributes: NSCollectionViewLayoutAttributes?
  10. var lineColor: NSColor = .systemBlue
  11. var lineHeight: CGFloat = 4.0
  12. override func layoutAttributesForElements(in rect: NSRect) -> [NSCollectionViewLayoutAttributes] {
  13. var attributes = super.layoutAttributesForElements(in: rect)
  14. // 将指示线添加到布局属性中
  15. if let lineAttributes = lineAttributes {
  16. attributes.append(lineAttributes)
  17. }
  18. return attributes
  19. }
  20. func showLine(at indexPath: IndexPath, below rect: NSRect) {
  21. let lineAttributes = NSCollectionViewLayoutAttributes(forDecorationViewOfKind: NSUserInterfaceItemIdentifier("Line").rawValue, with: indexPath)
  22. lineAttributes.frame = NSRect(x: rect.origin.x, y: rect.origin.y, width: rect.width, height: lineHeight)
  23. self.lineAttributes = lineAttributes
  24. invalidateLayout()
  25. }
  26. func hideLine() {
  27. lineAttributes = nil
  28. invalidateLayout()
  29. }
  30. }