简体   繁体   中英

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?

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

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

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 sales@agicent.com . We will try our best to resolve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM