Cocoa touch 如何在TouchsBegind功能中获取触摸的位置

Cocoa touch 如何在TouchsBegind功能中获取触摸的位置,cocoa-touch,Cocoa Touch,我尝试了下面的代码,但不起作用。如何修改 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { location = [touches locationInView:self]; } 在h文件中,我定义了如下位置: CGPoint location; (NSSet*)触摸将为您提供屏幕上所有当前触摸。你需要在这台电视机上进行每一次触摸,并获得它的坐标 这些触摸是UITouch类的成员。看一看这张照片 例如

我尝试了下面的代码,但不起作用。如何修改

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    location = [touches locationInView:self];
}
在h文件中,我定义了如下位置:

CGPoint location;
(NSSet*)触摸将为您提供屏幕上所有当前触摸。你需要在这台电视机上进行每一次触摸,并获得它的坐标

这些触摸是UITouch类的成员。看一看这张照片

例如,您将执行以下操作:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInView:self];

        // Do something with this location
        // If you want to check if it was on a specific area of the screen for example
        if (CGRectContainsPoint(myDefinedRect, location)) {
            // The touch is inside the rect, do something with it
            // ...
            // If one touch inside the rect is enough, break the loop
            break;
        }
    }
}

干杯

有没有一种方法可以直接访问第一次触摸,这样就不需要使用for循环?您可以在for循环结束时设置一个中断,使其只运行一次。或者你可以得到@Vladimir建议的任何对象。