[英]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.