BKButton.swift 941 B

12345678910111213141516171819202122232425262728293031323334
  1. //
  2. // BKButton.swift
  3. // Bark
  4. //
  5. // Created by huangfeng on 2020/9/23.
  6. // Copyright © 2020 Fin. All rights reserved.
  7. //
  8. import UIKit
  9. protocol AlignmentRectInsetsOverridable: AnyObject {
  10. var alignmentRectInsetsOverride: UIEdgeInsets? { get set }
  11. }
  12. protocol HitTestSlopable: AnyObject {
  13. var hitTestSlop: UIEdgeInsets { get set }
  14. }
  15. class BKButton: UIButton, HitTestSlopable, AlignmentRectInsetsOverridable {
  16. var hitTestSlop = UIEdgeInsets.zero
  17. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  18. if hitTestSlop == UIEdgeInsets.zero {
  19. return super.point(inside: point, with: event)
  20. }
  21. else {
  22. return self.bounds.inset(by: hitTestSlop).contains(point)
  23. }
  24. }
  25. var alignmentRectInsetsOverride: UIEdgeInsets?
  26. override var alignmentRectInsets: UIEdgeInsets {
  27. return alignmentRectInsetsOverride ?? super.alignmentRectInsets
  28. }
  29. }