如何以编程方式发送UILongPressGesture? [英] How can I send a UILongPressGesture programmatically?

查看:93
本文介绍了如何以编程方式发送UILongPressGesture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何向对象发送 UILongPressGesture 消息?



我知道如何发送触摸,但是不是手势。例如,如果我想发送一个我可以使用的触摸:

  [button sendActionsForControlEvents:UIControlEventTouchUpInside]; 

按钮会收到内部修饰。我需要用长按手势做同样的事情。



考虑自动化UI测试。调用与手势相关联的选择器不会有意义地测试任何东西。

解决方案

导入< UIKit / UIGestureRecognizerSubclass.h> 并根据您需要模拟的状态序列手动设置 state 属性。这将导致调用添加的目标/操作对。每次手动设置状态后,必须让运行循环运行,以便分派消息。



对于 UILongPressGestureRecognizer ,为了获得正确的状态序列,如实际的触摸,保持,拖动,释放手势序列,我在 UIViewController中编写了以下代码 中的子类viewDidLoad 。:

  UILongPressGestureRecognizer * r = [[UILongPressGestureRecognizer alloc] init]; 
[self.view addGestureRecognizer:r];
[r addTarget:self action:@selector(recognition :)];
r.state = UIGestureRecognizerStateBegan;
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
r.state = UIGestureRecognizerStateChanged;
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
r.state = UIGestureRecognizerStateEnded;
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
[r reset];

我想这在生产代码中会有风险(之后你可能希望打电话给重置但我在测试中发现没有区别,但是如果您的用例是自动测试以验证目标和操作是否已正确设置,则可能满足您的需求。 / p>

How can I send a UILongPressGesture message to an object?

I know how to send touches, but not gestures. For example, if I wanted to send a touch I could use:

[button sendActionsForControlEvents: UIControlEventTouchUpInside];

and the button would receive a "touch up inside". I need to do the same thing with a long press gesture.

Think automated UI testing. Calling a selector associated with the gesture wouldn't meaningfully test anything.

解决方案

Import <UIKit/UIGestureRecognizerSubclass.h> and manually set the state property as appropriate to the sequence of states you need to simulate. This will cause the added target/action pairs to be called. After each time manually setting the state, you must let the run loop run in order for the messages to be dispatched.

For the UILongPressGestureRecognizer, to get the correct sequence of states as is found in an actual, 'touch, hold, drag, release' sequence of gestures, I wrote the following code in a UIViewController subclass inside viewDidLoad.:

UILongPressGestureRecognizer *r = [[UILongPressGestureRecognizer alloc] init];
[self.view addGestureRecognizer:r];
[r addTarget:self action:@selector(recognize:)];
r.state = UIGestureRecognizerStateBegan;
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
r.state = UIGestureRecognizerStateChanged;
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
r.state = UIGestureRecognizerStateEnded;
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
[r reset];

I imagine this would be risky in production code (you may wish afterwards to call reset but I found no difference between doing so or not in my testing), but if your use case is automated testing to verify that the targets and actions have been set correctly this may meet your needs.

这篇关于如何以编程方式发送UILongPressGesture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆