简体   繁体   English

如何获得手指坐标

[英]How to get finger coordinates

I can not find how I can get the coordinates of the mouse/finger on an iphone in Xcode. 我找不到如何在Xcode中获取iPhone上的鼠标/手指坐标的方法。

I tried: 我试过了:

NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation];

But this seems not to work for iphone? 但这似乎不适用于iPhone? The API does not recognise it. API无法识别它。

Does anyone know a way to do this? 有人知道这样做的方法吗?

Here is Apple's doc: UIResponder Class Reference . 这是Apple的文档: UIResponder类参考 And in a UIView or UIViewController 's subclass you can add this method: UIViewUIViewController的子类中,可以添加以下方法:

- (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. 您正在使用可可类。对于iPhone,有cocoa.touch,您需要它是UIEvent。
The method allTouches returns all the touches events for the window.Once got a touch you can ask it's location in view. allTouches方法返回窗口的所有touches事件。一旦触摸,您可以询问其在视图中的位置。

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. 您可以使用UITapGestureRecognizer识别手指在屏幕上的触摸位置。 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对象/插座,例如在我的示例中,我在View上添加了UITapGestureRecognizer。

    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 . 如有其他查询,请联系我们sales@agicent.com。 We will try our best to resolve. 我们将尽力解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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