how to programmatically fake a touch event to a UIButton?(如何以编程方式将触摸事件伪造为 UIButton?)
问题描述
我正在编写一些单元测试,并且由于这个特定应用程序的性质,重要的是我要在 UI 链中尽可能高.所以,我想做的是以编程方式触发按钮按下,就好像用户按下了 GUI 中的按钮一样.
I'm writing some unit tests and, because of the nature of this particular app, it's important that I get as high up the UI chain as possible. So, what I'd like to do is programmatically trigger a button-press, as if the user had pressed the button in the GUI.
(是的,是的——我可以只调用 IBAction 选择器,但同样,这个特定应用程序的性质使得我假装实际按下按钮很重要,以便从按钮本身调用 IBAction.)
(Yes, yes -- I could just call the IBAction selector but, again, the nature of this particular app makes it important that I fake the actual button press, such that the IBAction be called from the button, itself.)
这样做的首选方法是什么?
What's the preferred method of doing this?
推荐答案
原来如此
[buttonObj sendActionsForControlEvents:UIControlEventTouchUpInside];
在这种情况下,我得到了我需要的东西.
got me exactly what I needed, in this case.
不要忘记在主线程中执行此操作,以获得类似于用户按下的结果.
Don't forget to do this in the main thread, to get results similar to a user-press.
对于 Swift 3:
buttonObj.sendActions(for: .touchUpInside)
这篇关于如何以编程方式将触摸事件伪造为 UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以编程方式将触摸事件伪造为 UIButton?
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
