-3

I want to simulate a mouse pointer and click event on any object in the view. Until now I am able to simulate a mouse pointer i.e. The mouse pointer moves by dragging it on the view. Now I want to fire tap event on the underlying object on which the simulated mouse pointer is clicked. Suppose a button. The concept is about a finger mouse.

Sharon Nathaniel
  • 1,467
  • 20
  • 28

4 Answers4

1

I got you,

1) add a UITapGestureRecognizer to your mouse pointer.

2) in tapgesture handle method,

get the finger point,

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

Make a fingerRect like this,

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

iterate through all the uiviews in the self.view

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

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

}

Finally method will be like this,

-(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;
        }

    }

}

That way you can detect the touched view, depending on the view you can call methods you want.

Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
0

with a bit of hacking and looking at the KIF testing framework:

https://github.com/square/KIF/blob/master/Additions/UIView-KIFAdditions.m here is a UIView::tapAtPoint method & an _eventWithTouch method

look at more code there .. it isn't all public documented API though so you are likely to get rejected for this :D

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
-1

Do you need to do this so you can run automated testing of an iOS app? If so, this thread is quite old but has links to relevent material. Stack Overflow 402389

Community
  • 1
  • 1
user16259
  • 101
  • No I am not trying to test user interface. Just imagine iPad as your screen that is at some distance and you have a mouse wearable on your finger. – Sharon Nathaniel Apr 25 '13 at 12:06
-2

You cannot simulate touches directly!!!! That's why there is such thing as testing. You either do the deed( run the app, click and move ) or not.

Also..it's called touch, not click

skytz
  • 2,201
  • 2
  • 18
  • 23