Created
April 19, 2021 20:33
-
-
Save leoniralves/29d4575eb6689d2d0b6604d48a6f0112 to your computer and use it in GitHub Desktop.
Simulate Gesture on Views in Swift (draft)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
private(set) var tap: UITapGestureRecognizer | |
init(tap: UITapGestureRecognizer) { | |
self.tap = tap | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tap.addTarget(self, action: #selector(tapButton)) | |
self.view.addGestureRecognizer(tap) | |
} | |
@objc private func tapButton() { | |
print("Tap Gesture Ok") | |
} | |
} | |
final class UITapGestureRecognizerFake: UITapGestureRecognizer { | |
private var testTarget: Any? = nil | |
private var testAction: Selector? = nil | |
init() { | |
super.init(target: nil, action: nil) | |
} | |
override func addTarget(_ target: Any, action: Selector) { | |
testTarget = target | |
testAction = action | |
} | |
func perfomrTouch() { | |
guard let selector = self.testAction else { return } | |
(testTarget as AnyObject).perform(selector, on: Thread.current, with: self, waitUntilDone: true) | |
} | |
} | |
let tap = UITapGestureRecognizerFake() | |
let viewController = ViewController(tap: tap) | |
viewController.view.layoutIfNeeded() | |
let t = viewController.tap as? UITapGestureRecognizerFake | |
t?.perfomrTouch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muito útil