尝试获取触摸点,返回 NaN

发布于 2024-12-19 06:12:00 字数 642 浏览 0 评论 0原文

好吧,我在这里遇到了一个非常奇怪的(对我来说)问题。我的视图中有一个 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 技术交流群。

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

发布评论

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

评论(2

蓝海 2024-12-26 06:12:00

UIGestureRecognizer 在其生命周期中有多种状态,例如可能、已识别、失败、结束、取消。我会尝试在状态识别器方法中放置一个 switch 语句,看看发生了什么,以更好地缩小问题范围。它看起来像这样:

switch (sender.state){
    case  UIGestureRecognizerStatePossible:
        NSLog(@"possible");
        break;
    case  UIGestureRecognizerStateFailed:
        NSLog(@"Failed!");
        break;
    ... All Cases wanted
    default:
        break;
}

我不知道内部细节,但也许如果它失败/取消,它不会拾取视图中的位置。

这是有关手势识别器子类化以及可能发生的状态的文档。

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:

switch (sender.state){
    case  UIGestureRecognizerStatePossible:
        NSLog(@"possible");
        break;
    case  UIGestureRecognizerStateFailed:
        NSLog(@"Failed!");
        break;
    ... All Cases wanted
    default:
        break;
}

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

简单爱 2024-12-26 06:12:00

我尝试重现您的问题,但没有成功。起初,我认为手势识别器可能会干扰scrollView滚动手势识别器,但事实似乎并非如此。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongHold:)];
//    [scrollView addGestureRecognizer:recognizer];
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 2000)];
    lbl.text = @"lorem ipsum......";
    lbl.numberOfLines = 0;
    scrollView.contentSize = CGSizeMake(lbl.frame.size.width, lbl.frame.size.height);
    [scrollView addSubview:lbl];
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(0, 0, 150, 150);
    [btn addGestureRecognizer:recognizer];
    [scrollView addSubview:btn];

    v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    v.backgroundColor = [UIColor yellowColor];
    v.hidden = YES;
    [scrollView addSubview:v];
}

- (void) handleLongHold:(UILongPressGestureRecognizer*) recognizer {
    NSLog(@"long tap handled");
    v.hidden = NO;
    v.center = [recognizer locationInView:btn];
}

顺便说一句,我偶然发现了您的问题,寻找一种从手势识别器选择器中查找触摸点的方法 - 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:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongHold:)];
//    [scrollView addGestureRecognizer:recognizer];
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 2000)];
    lbl.text = @"lorem ipsum......";
    lbl.numberOfLines = 0;
    scrollView.contentSize = CGSizeMake(lbl.frame.size.width, lbl.frame.size.height);
    [scrollView addSubview:lbl];
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(0, 0, 150, 150);
    [btn addGestureRecognizer:recognizer];
    [scrollView addSubview:btn];

    v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    v.backgroundColor = [UIColor yellowColor];
    v.hidden = YES;
    [scrollView addSubview:v];
}

- (void) handleLongHold:(UILongPressGestureRecognizer*) recognizer {
    NSLog(@"long tap handled");
    v.hidden = NO;
    v.center = [recognizer locationInView:btn];
}

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.

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