尝试获取触摸点,返回 NaN
好吧,我在这里遇到了一个非常奇怪的(对我来说)问题。我的视图中有一个 150x150 的按钮,并且我向该按钮添加了一个 UILongPressGestureRecognizer
,因为我需要在按下按钮时获取按钮被按下的点。我的代码如下所示:
-(CGPoint)detectedTouch:(UILongPressGestureRecognizer *)sender {
CGPoint touchPoint = [sender locationInView:button];
return touchPoint;
}
-(void)myAction {
CGPoint touchPoint = [self detectedTouch:myGestureRecognizer];
NSLog(@"touchPoint = %f, %f", touchPoint.x, touchPoint.y);
//do stuff
}
现在,当按钮位于普通视图上时,一切正常。但是当按钮位于滚动视图上时,只有当您按下它大约一秒钟时它才会起作用。如果您释放得太快,日志会给我这样的信息:
touchPoint = nan, nan
任何解决此问题的帮助将不胜感激!
Okay, I'm running into a very strange (for me) problem here. I have a 150x150 button in my view and I've added a UILongPressGestureRecognizer
to that button, because I need to get the point where the button has been pressed when it is pressed down. My code to do that looks like this:
-(CGPoint)detectedTouch:(UILongPressGestureRecognizer *)sender {
CGPoint touchPoint = [sender locationInView:button];
return touchPoint;
}
-(void)myAction {
CGPoint touchPoint = [self detectedTouch:myGestureRecognizer];
NSLog(@"touchPoint = %f, %f", touchPoint.x, touchPoint.y);
//do stuff
}
Now everything works just fine when the button is on a normal view. But when the button is on a scrollView, it only works when you press it down for about a second. If you release it too quickly, the log gives me this:
touchPoint = nan, nan
Any help to solve this problem would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIGestureRecognizer
在其生命周期中有多种状态,例如可能、已识别、失败、结束、取消。我会尝试在状态识别器方法中放置一个 switch 语句,看看发生了什么,以更好地缩小问题范围。它看起来像这样:我不知道内部细节,但也许如果它失败/取消,它不会拾取视图中的位置。
这是有关手势识别器子类化以及可能发生的状态的文档。
http://developer.apple.com/库/IOs/#documentation/UIKit/Reference/UIGestureRecognizer_Class/Reference/Reference.html
UIGestureRecognizer
have various states in their life span, such as possible, recognized, failed, ended, canceled. I would try putting a switch statement inside your recognizer method on the state and see which is happening to better narrow down the problem. It would look something like this:I don't know the internal details but perhaps if it fails/cancels it doesn't pick up the location in the view.
Here is the docs on subclassing gesture recognizers and the states that can happen.
http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIGestureRecognizer_Class/Reference/Reference.html
我尝试重现您的问题,但没有成功。起初,我认为手势识别器可能会干扰scrollView滚动手势识别器,但事实似乎并非如此。这是我的代码:
顺便说一句,我偶然发现了您的问题,寻找一种从手势识别器选择器中查找触摸点的方法 - locationInView 方法帮助了我,希望这段代码对您有所帮助。
I tried to reproduce your problem, but was not successful. At first, I thought the gesture recognizer might be interfering with the scrollView scroll gesture recognizer, but that doesn't seem to be the case. Here is the code I have:
By the way, I stumbled on your question looking for a way to find a touch point from a gesture recognizer selector - the locationInView method helped me, and hopefully this code helps you.