尝试解析触摸位置始终返回零 (0) 作为 x 位置
在为我的视图编写 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有机会使用 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.
本是对的。 问题是当您尝试在
NSLog
中使用%d
打印值时,因为point.x
和point.y 是浮点值。
Ben is right. The problem is when you try to print the value using
%d
inNSLog
, becausepoint.x
andpoint.y
are float values.