从touchesBegan获取触摸位置?(以及其他游戏问题)

7

我想从 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 中获取触摸事件在屏幕上的位置。

请问我可以使用什么代码来读取触摸坐标?到目前为止,我已经做到了这一步:

NSLog(@"Touch data: %@", [NSString stringWithFormat:@"NSSet: %@",[touches description]]);

我正在使用一个OpenGL模板应用程序制作游戏。触摸事件只能在EAGLView文件中注册,而不是ESRenderer1文件中。我认为我应该使用EAGlView设置变量,然后从那里读取变量到ESRenderer中进行游戏计算。这种方法好吗?

1个回答

17
下面的代码可以实现此功能:
NSArray *touchesArray = [touches allObjects];
for(int i=0; i<[touchesArray count]; i++)
{
    UITouch *touch = (UITouch *)[touchesArray objectAtIndex:i];
    CGPoint point = [touch locationInView:nil];

    // do something with 'point'
}

5
我会选择使用for (UITouch *touch in touches) {/*...*/}。(快速枚举!) - bddckr
如果我只想要最新的触摸信息,而不需要其他信息呢?这该怎么实现? - Moshe
我认为数组是“最新”触摸列表,它表示手指当前在屏幕上的位置。大多数情况下,对于大多数应用程序,该数组只有一个元素。 - Jim Buck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接