模拟光标并单击

时间:2013-04-25 11:47:17

标签: iphone ios ipad

我想在视图中的任何对象上模拟鼠标指针和单击事件。到目前为止,我能够模拟鼠标指针,即鼠标指针通过在视图上拖动来移动。现在我想在点击模拟鼠标指针的底层对象上触发tap事件。假设一个按钮。这个概念是关于手指鼠标。

4 个答案:

答案 0 :(得分:1)

我找到了你,

1)在鼠标指针上添加UITapGestureRecognizer

2)在tapgesture句柄方法中,

得到手指点,

CGPoint fingerlocation = [tap locationInView:self.view];

像这样做一个fingerRect,

CGRect finderRect = CGRectMake(location.x - 5, location.y - 5, 10, 10);

遍历self.view

中的所有uiviews
  for (UIView *view in self.view.subviews) {
    CGRect frameOfTheView = view.frame;

    if(CGRectIntersectsRect(frameOfTheView, finderRect)){
        NSLog(@"clicked on view %@", view);
        return;
    }

}

最后方法将是这样的,

-(void) handleTap:(UITapGestureRecognizer *) tap{

    //get the tapped point
    CGPoint location = [tap locationInView:self.view];
    CGRect finderRect = CGRectMake(location.x - 5, location.y - 5, 10, 10);

    for (UIView *view in self.view.subviews) {
        CGRect frameOfTheView = view.frame;

        if(CGRectIntersectsRect(frameOfTheView, finderRect)){
            NSLog(@"clicked on view %@", view);
            return;
        }

    }

}

这样您就可以检测到触摸的视图,具体取决于您可以调用所需方法的视图。

答案 1 :(得分:0)

有点黑客攻击并查看KIF测试框架:

https://github.com/square/KIF/blob/master/Additions/UIView-KIFAdditions.m 这是UIView::tapAtPoint方法&一个_eventWithTouch方法

在那里查看更多代码..虽然不是所有公开记录的API,但您可能会因此被拒绝:D

答案 2 :(得分:-1)

您是否需要执行此操作以便对iOS应用程序进行自动测试?如果是这样,这个线程很老,但有相关资料的链接。 Stack Overflow 402389

答案 3 :(得分:-2)

你不能直接模拟触摸!!!!这就是测试的原因。你要么做(或运行应用程序,点击并移动),否则。

另外..这叫做触摸,而不是点击

相关问题