BlockTap.swift 1018 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // BlockTap.swift
  3. // Runner
  4. //
  5. // Created by FancyLou on 2023/5/23.
  6. //
  7. //
  8. #if os(iOS) || os(tvOS)
  9. import UIKit
  10. ///Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
  11. open class BlockTap: UITapGestureRecognizer {
  12. private var tapAction: ((UITapGestureRecognizer) -> Void)?
  13. public override init(target: Any?, action: Selector?) {
  14. super.init(target: target, action: action)
  15. }
  16. public convenience init (
  17. tapCount: Int = 1,
  18. fingerCount: Int = 1,
  19. action: ((UITapGestureRecognizer) -> Void)?) {
  20. self.init()
  21. self.numberOfTapsRequired = tapCount
  22. #if os(iOS)
  23. self.numberOfTouchesRequired = fingerCount
  24. #endif
  25. self.tapAction = action
  26. self.addTarget(self, action: #selector(BlockTap.didTap(_:)))
  27. }
  28. @objc open func didTap (_ tap: UITapGestureRecognizer) {
  29. tapAction? (tap)
  30. }
  31. }
  32. #endif