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.
-
As I have mentioned it a concept of finger mouse. Just like a desktop mouse. – Sharon Nathaniel Apr 25 '13 at 11:57
-
rename the title. I get what you want but iOS has no mouse so the title is confusing – Daij-Djan Apr 25 '13 at 12:13
-
This is very confusing on what you want. Title, question content are all confusing. -1. Will remove -1 if question is made more understandable. – Popeye Apr 25 '13 at 12:58
-
I am really sorry cannot express my aim of the question very well. – Sharon Nathaniel Apr 26 '13 at 03:55
4 Answers
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.

- 9,039
- 3
- 32
- 45
-
this is nice .. and to me the cleaner way as it doesn't use private API like I sugggested – Daij-Djan Apr 25 '13 at 13:10
-
Thanks will try this out and will post my feedback. Thanks once again – Sharon Nathaniel Apr 26 '13 at 03:53
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

- 49,552
- 17
- 113
- 135
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
-
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
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

- 2,201
- 2
- 18
- 23