I'm trying to fake pan gesture, but am not sure how to do it. There is some complicated code determining what happens with the pan gesture, so before going in and abstracting out all the logic, I was wondering if anybody knew of a way to fake the gesture. Thanks.

有帮助吗?

解决方案

Unfortunately the API does not provide a way to trigger the gesture programmatically nor does it provide public access to the targets and selectors of the UIGestureRecognizer.

However, if you are wanting to do this for unit tests, you could get access to the targets and selectors by doing the following:

NSArray *targets = [panGesture valueForKey:@"_targets"];
NSArray *actions = [panGesture valueForKey:@"_actions"];

You can then loop through those arrays and call the action on the target:

[target performSelector:selector withObject:panGesture];

其他提示

To "fake a pan gesture" I assume you mean move the view somewhere it currently isn't. UIView animations can be used for this.

[UIView beginAnimations:@"Fake-A-Pan" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
self.frame = CGRectMake(/* new coordinates for frame here */);
[UIView commitAnimations];  

By using an animation to change the location of the view, you can essentially "fake a pan". Edit As a comment suggested, Block-Based animations can also be used.

[UIView animateWithDuration:.5f 
                      delay:0.0 
                    options:UIViewAnimationOptionCurveEaseInOut 
                 animations:^ {
                     self.frame ... // adjust your view here 
                 } 
                 completion:^(BOOL finished) {
                     NSLog(@"Done animating");
                 }];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top