尝试解析触摸位置始终返回零 (0) 作为 x 位置

发布于 2024-07-25 00:14:51 字数 367 浏览 7 评论 0原文

在为我的视图编写 TouchBegan 处理程序时,我遇到了这个有趣的错误。

代码:

...
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
...

问题是 point.x 总是零,无论我“触摸”哪里,y 都是一个巨大的数字,在 1070000000 和 1090000000 之间波动。

句柄位于视图类中,如果我传递 nil ,它的工作原理是相同的self 到 locationInView。 我还尝试循环所有触摸,并且只有 x 始终为零的触摸。

那么我在这里做错了什么?

While writing the touchesBegan handler for my view I ran into this interesting error.

Code:

...
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
...

The problem is point.x is always zero, no matter where I "touch" and y is a huge number that flunctuates somewhere between 1070000000 and 1090000000.

The handles is in a view class and it works the same is I pass nil instead of self to locationInView. I also tried looping over all of the touches, and there is only that one with x as zero always.

So what am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦境 2024-08-01 00:14:51

您有机会使用 NSLog(@"%d, %d", point.x, point.y); 打印出点值? 听起来有点像漂浮物 -> int 转换错误。 我经常使用 locationInView: ,但从未遇到过这样的问题。

您也可以尝试检查 [self frame] 以确保它不为零。

Any chance you're using NSLog(@"%d, %d", point.x, point.y); to print out the point values? It sounds kind of like a float -> int conversion error. I've used locationInView: a lot, and I've never had a problem like this.

You might also try checking [self frame] to make sure it's nonzero.

好多鱼好多余 2024-08-01 00:14:51

本是对的。 问题是当您尝试在 NSLog 中使用 %d 打印值时,因为 point.xpoint.y 是浮点值。

Ben is right. The problem is when you try to print the value using %d in NSLog, because point.x and point.y are float values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文