BlockSwipe.swift 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // BlockSwipe.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 BlockSwipe: UISwipeGestureRecognizer {
  12. private var swipeAction: ((UISwipeGestureRecognizer) -> Void)?
  13. public override init(target: Any?, action: Selector?) {
  14. super.init(target: target, action: action)
  15. }
  16. public convenience init (
  17. direction: UISwipeGestureRecognizer.Direction,
  18. fingerCount: Int = 1,
  19. action: ((UISwipeGestureRecognizer) -> Void)?) {
  20. self.init()
  21. self.direction = direction
  22. #if os(iOS)
  23. numberOfTouchesRequired = fingerCount
  24. #endif
  25. swipeAction = action
  26. addTarget(self, action: #selector(BlockSwipe.didSwipe(_:)))
  27. }
  28. @objc open func didSwipe (_ swipe: UISwipeGestureRecognizer) {
  29. swipeAction? (swipe)
  30. }
  31. }
  32. #endif