简体   繁体   中英

How to trigger tap gesture recognizer of UIView programmatically

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.

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.

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM