2

I want to get tap gesture recogniser attached to an UIView and trigger it programatically. I successfully found a way to find tap gesture recogniser of an UIView. But now I don't know how to trigger it programatically. I am working on app crawler. So if I have a source code of a ViewController then, my crawler will start crawling find all the subviews of ViewController. Also the crawler will find gesture attached to the subviews. And trigger tap gesture.

Note : I know how to attach gesture recogniser to UIView and trigger it. But in my case I wanted to automate the clicking process of an app. Just consider as I want to runtime find tap gesture recogniser of UIView and trigger it.

techcraze
  • 112
  • 1
  • 10

3 Answers3

5

I solved this by creating fake touches. Triggering gesture was not possible as gestures does not expose its action and selector. But as I have UIView on which I want to tap. I can create a fake touch on that UIView. This fake touch will automatically trigger the tap gesture. This link was very helpful for this.

techcraze
  • 112
  • 1
  • 10
1

You can't, because the gesture doesn't expose its target and action. You'd need to either create a subclass that did or use some private API to access those values so you could use them. If you're creating something general then the subclass option is no good anyway, but more broadly than that not everything works with gestures so your solution is incomplete.

I think most test tools use the accessibility interface to find interactive views and programmatically tap them.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

techcraze brother generally Gesture Says

Gestures are actually touches and movements of one or more fingers that happen on a specific area of the screen, where a view of interest exists there

When it comes to Tap gesture Recognizer

UITapGestureRecognizer: This class regards the tap gestures made on a view. It can be used to handle single or multiple taps, either with one or more fingers. Tapping is one of the most usual gestures that users make.

without finger help you can't do automatically brother.

So our code go with

- (void)viewDidLoad
{
  UITapGestureRecognizer *tapgesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
  tapgesture.numberOfTouchesRequired=1;
  [self.view addGestureRecognizer:tapgesture];
}

-(void)tapView:(UITapGestureRecognizer *)gesture
{
  NSLog(@"The taped tag view is - %ld",gesture.view.tag);
}
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • i know this brother. What i want to find out is when user taps on the screen, there must be some method which will recognize the tap and will trigger some method. i want to find out which method is used by CocoTouch so that it finds the view tapped and triggers the gesture. – techcraze Aug 30 '16 at 06:57
  • brother when you tap on view the tapView: method calls automatically.See action:@selector(tapView:).Put put breakpoint in your tapView method.It calls once tap the view. – user3182143 Aug 30 '16 at 07:04
  • yes. But what if you dont know the what action it is attached to? In my case i will have only gesture of view. And from gesture i have to either find its action or any way to trigger without finding its action. I think @Wain's answer makes sense that gesture dont expose its target and action. – techcraze Aug 30 '16 at 07:09
  • Congratulations brother:-) – user3182143 Sep 02 '16 at 09:56
  • How did you achieve this? – user3182143 Sep 02 '16 at 09:56