Senior Me
Senior Me

Reputation: 87

How to get finger coordinates

I can not find how I can get the coordinates of the mouse/finger on an iphone in Xcode.

I tried:

NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation];

But this seems not to work for iphone? The API does not recognise it.

Does anyone know a way to do this?

Upvotes: 0

Views: 2758

Answers (3)

indu
indu

Reputation: 323

You can use UITapGestureRecognizer to recognize where finger touched on the screen. For this you have to add one UITapGestureRecognizer object/outlet on control where you want to handle touch such as in my example i am adding UITapGestureRecognizer on View.

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTappedHere:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tapGesture];
self.view.userInteractionEnabled = YES;


- (void)fingerTappedHere:(UITapGestureRecognizer *)sender {

CGPoint touchedPoint = [sender locationInView:self.view];
NSLog(@"x = %f, y = %f", touchedPoint.x, touchedPoint.y);

}

Hope it will help you. For further queries contact us at [email protected] . We will try our best to resolve.

Upvotes: 1

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 22006

You are using cocoa classes.For iPhone there is cocoa.touch, the class you need it UIEvent.
The method allTouches returns all the touches events for the window.Once got a touch you can ask it's location in view.

Example:

void event : (UIEvent*) event
{
    NSSet* touches=[event allTouches];
    for(UITouch* touch in touches)
    {
        CGPoint point=[touch locationInView: yourView];
        <Do what you need to do with the point>
    }
}

Upvotes: 2

sunkehappy
sunkehappy

Reputation: 9101

Here is Apple's doc: UIResponder Class Reference. And in a UIView or UIViewController's subclass you can add this method:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touched = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touched.view];
    NSLog(@"x=%.2f y=%.2f", location.x, location.y);
}

Upvotes: 4

Related Questions