BlockLongPress.swift 934 B

12345678910111213141516171819202122232425262728293031323334
  1. //
  2. // BlockLongPress.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 BlockLongPress: UILongPressGestureRecognizer {
  12. private var longPressAction: ((UILongPressGestureRecognizer) -> Void)?
  13. public override init(target: Any?, action: Selector?) {
  14. super.init(target: target, action: action)
  15. }
  16. public convenience init (action: ((UILongPressGestureRecognizer) -> Void)?) {
  17. self.init()
  18. longPressAction = action
  19. addTarget(self, action: #selector(BlockLongPress.didLongPressed(_:)))
  20. }
  21. @objc open func didLongPressed(_ longPress: UILongPressGestureRecognizer) {
  22. if longPress.state == UIGestureRecognizer.State.began {
  23. longPressAction?(longPress)
  24. }
  25. }
  26. }
  27. #endif