1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- //
- // BlockTap.swift
- // Runner
- //
- // Created by FancyLou on 2023/5/23.
- //
- //
- #if os(iOS) || os(tvOS)
- import UIKit
- ///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
- open class BlockTap: UITapGestureRecognizer {
- private var tapAction: ((UITapGestureRecognizer) -> Void)?
- public override init(target: Any?, action: Selector?) {
- super.init(target: target, action: action)
- }
- public convenience init (
- tapCount: Int = 1,
- fingerCount: Int = 1,
- action: ((UITapGestureRecognizer) -> Void)?) {
- self.init()
- self.numberOfTapsRequired = tapCount
- #if os(iOS)
- self.numberOfTouchesRequired = fingerCount
- #endif
- self.tapAction = action
- self.addTarget(self, action: #selector(BlockTap.didTap(_:)))
- }
- @objc open func didTap (_ tap: UITapGestureRecognizer) {
- tapAction? (tap)
- }
- }
- #endif
|