Skip to content

Instantly share code, notes, and snippets.

@leoniralves
Created April 19, 2021 20:33
Show Gist options
  • Save leoniralves/29d4575eb6689d2d0b6604d48a6f0112 to your computer and use it in GitHub Desktop.
Save leoniralves/29d4575eb6689d2d0b6604d48a6f0112 to your computer and use it in GitHub Desktop.
Simulate Gesture on Views in Swift (draft)
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()
@magnoFr
Copy link

magnoFr commented Oct 1, 2021

Muito útil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment