UITapGestureRecognizer以编程方式触发我视图中的点按 [英] UITapGestureRecognizer Programmatically trigger a tap in my view

查看:82
本文介绍了UITapGestureRecognizer以编程方式触发我视图中的点按的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:更新以使问题更加明显

编辑2:使问题更准确地解决我的实际问题。我实际上是想采取行动,如果他们在屏幕上的文本字段除外。因此,我不能简单地听取文本字段中的事件,我需要知道他们是否在视图中点击任何地方

Edit 2: Made question more accurate to my real-world problem. I'm actually looking to take action if they tap anywhere EXCEPT in an on-screen text-field. Thus, I can't simply listen for events within the textfield, I need to know if they tapped anywhere in the View.

我正在编写单元测试来声明当手势识别器识别出我视图的某些坐标中的点击时会采取某种操作。我想知道我是否可以通过编程方式创建将由UITapGestureRecognizer处理的触摸(在特定坐标处)。 我正在尝试在单元测试期间模拟用户交互。

I'm writing unit tests to assert that a certain action is taken when a gesture recognizer recognizes a tap within certain coordinates of my view. I want to know if I can programmatically create a touch (at specific coordinates) that will be handled by the UITapGestureRecognizer. I'm attempting to simulate the user interaction during a unit test.

UITapGestureRecognizer在Interface Builder中配置

The UITapGestureRecognizer is configured in Interface Builder

//MYUIViewControllerSubclass.m

-(IBAction)viewTapped:(UITapGestureRecognizer*)gesture {
  CGPoint tapPoint = [gesture locationInView:self.view];
  if (!CGRectContainsPoint(self.textField, tapPoint)) {
    // Do stuff if they tapped anywhere outside the text field
  }
}

//MYUIViewControllerSubclassTests.m
//What I'm trying to accomplish in my unit test:

-(void)testThatTappingInNoteworthyAreaTriggersStuff {
  // Create fake gesture recognizer and ViewController
  MYUIViewControllerSubclass *vc = [[MYUIViewControllersSubclass alloc] init];
  UITapGestureRecognizer *tgr = [[UITapGestureRecognizer initWithView: vc.view];

  // What I want to do:
  [[ Simulate A Tap anywhere outside vc.textField ]]
  [[  Assert that "Stuff" occured ]]
}


推荐答案

我认为你有多种选择:


  1. 可能最简单的方法是将推送事件操作发送给您查看,但我不认为你真正想要的,因为你希望能够选择点击动作发生的位置。

  1. May be the simplest would be to send a push event action to your view but i don't think that what you really want since you want to be able to choose where the tap action occurs.


[yourView sendActionsForControlEvents:UIControlEventTouchUpInside];

[yourView sendActionsForControlEvents: UIControlEventTouchUpInside];


  • 您可以使用随XCode工具提供的 UI自动化工具。这个博客解释了如何自动化您的用户界面然后用脚本测试。

  • You could use UI automation tool that is provided with XCode instruments. This blog explains well how to automate your UI tests with script then.

    这是解决方案也解释了如何在iPhone上合成触摸事件,但要确保只使用它们进行单元测试。这听起来更像是对我的破解,如果之前的两点不能满足您的需求,我会认为这个解决方案是最后的选择。

    There is this solution too that explain how to synthesize touch events on the iPhone but make sure you only use those for unit tests. This sounds more like a hack to me and I will consider this solution as the last resort if the two previous points doesn't fulfill your need.

    这篇关于UITapGestureRecognizer以编程方式触发我视图中的点按的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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