Import <UIKit/UIGestureRecognizerSubclass.h> and manually set the state property according to the sequence of states that you want to simulate. This will cause the added target / action pairs to be called. After each manual state, you must run a run loop to send messages.
For UILongPressGestureRecognizer , to get the correct sequence of states found in the actual gesture sequence "touch, hold, drag, release", I wrote the following code in a subclass of UIViewController inside viewDidLoad :.
UILongPressGestureRecognizer *r = [[UILongPressGestureRecognizer alloc] init]; [self.view addGestureRecognizer:r]; [r addTarget:self action:@selector(recognize:)]; r.state = UIGestureRecognizerStateBegan; [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; r.state = UIGestureRecognizerStateChanged; [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; r.state = UIGestureRecognizerStateEnded; [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; [r reset];
I believe that this would be risky in the production code (you may wish to call reset later, but I did not find the difference between doing this or not in my testing), but if your use case is automatic testing to make sure goals and actions were set correctly, it can satisfy your needs.
source share