2

我正在为 iPhone 和 iPad 编写一个由屏幕触摸控制的 OpenGL 游戏。这是一个通用应用程序,所以我在 touchesBegan 中有代码,可以将任何触摸位置转换为 0.0-1.0 范围(屏幕的一侧到另一侧,这是应用程序的其余部分需要获取触摸信息的方式)。执行此操作的代码是:

CGPoint touchLocationPx;
GLFloat xx;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    touchLocationPx = [touch locationInView:self];    // touchLocationPx.x & .y are in points, not pixels...
    xx = (GLfloat)touchLocationPx.x / (GLfloat)(self.frame.size.width);    // self.frame.size.width is also in points,
                                                                           // so xx is in the range 0.0 to 1.0...

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Set xx to a dummy value that will not be processed by the rest of the app...
    xx = -999.999;
}

它依赖于 [touch locationInView:self] 和 self.frame.size.width 总是在相同的单位(点而不是我理解的像素),无论它是非视网膜屏幕,视网膜屏幕与 [UIScreen mainScreen ].scale=2.0,或处于 x1 或 x2 模式的 iPad。(我自己的测试设备是 iPhone 4S,如果我监控屏幕尺寸和触摸位置,它都会按预期以点 (480,320) 报告。但是我听说 iPad3 上的控件没有响应。屏幕尺寸和触摸位置在这台设备上处理方式不同?还是有其他原因导致我的代码无法正确转换触摸位置?(在模拟器上,所有设备类型都可以正常工作,包括视网膜 iPad。我知道模拟器永远不会与真实设备完全匹配,但是像如何报告触摸位置这样基本的东西应该正确地存在。知道为什么真正的设备会失败吗?)

谢谢,

山姆。

4

0 回答 0