简体   繁体   English

如何以编程方式触发UIView的点击手势识别器

[英]How to trigger tap gesture recognizer of UIView programmatically

I want to get tap gesture recogniser attached to an UIView and trigger it programatically. 我想将轻击gesture识别器附加到UIView并以编程方式触发它。 I successfully found a way to find tap gesture recogniser of an UIView . 我成功地找到了一种找到UIView轻击手势识别器的方法。 But now I don't know how to trigger it programatically. 但是现在我不知道如何以编程方式触发它。 I am working on app crawler . 我正在开发应用crawler So if I have a source code of a ViewController then, my crawler will start crawling find all the subviews of ViewController . 因此,如果我有ViewController的源代码,那么我的搜寻器将开始搜寻以查找ViewController所有subviews Also the crawler will find gesture attached to the subviews . 爬虫还将找到附加到subviews gesture And trigger tap gesture . 并触发点击gesture

Note : I know how to attach gesture recogniser to UIView and trigger it. 注意:我知道如何将手势识别器附加到UIView并触发它。 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. 只是考虑一下,因为我想在运行时找到UIView点击手势识别器并触发它。

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. 但是,因为我有要点击的UIView I can create a fake touch on that UIView . 我可以在该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. 您需要创建一个做到这一点的子类,或者使用某些私有API来访问这些值,以便可以使用它们。 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 Techcraze兄弟总体上

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 谈到Tap手势识别器

UITapGestureRecognizer: This class regards the tap gestures made on a view. UITapGestureRecognizer:此类考虑在视图上进行 手势。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 swift 中以编程方式在 UIView 上调用手势点击 - How to call gesture tap on UIView programmatically in swift 如何以编程方式将手势识别器添加到自定义 UIView (XIB)? - How to add gesture recognizer to custom UIView (XIB) programmatically? 如何在 Swift 5 中的 UIScrollView 中以编程方式生成的 UIView 上实现点击手势 - How to Implement Tap Gesture on programmatically generated UIView inside UIScrollView in Swift 5 点击手势识别器不适用于UIVcrollView内的UIWebiew,它位于UIView内部 - Tap gesture recognizer Not working for UIWebiew inside UIScrollView,which is inside UIView 嵌入在超级视图中的自定义 UIView 中未收到点击手势识别器 - Tap Gesture Recognizer not received in custom UIView embedded in super view 如何识别调用哪个手势识别器? - how to identify which tap gesture recognizer is called? 点击手势识别器 - Tap Gesture Recognizer In Bounds 如何以手势识别器作为参数传递UIView? - How to pass the UIView with a gesture recognizer as parameter? MpMovieplayerController轻触手势识别器在全屏时不会触发 - MpMovieplayerController tap gesture recognizer doesn't trigger when in fullscreen UIView中用于UIImageView的手势识别器 - Gesture recognizer for UIImageView in UIView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM