BlockTap.swift 1009 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // BlockTap.swift
  3. //
  4. //
  5. // Created by Cem Olcay on 12/08/15.
  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