【问题标题】:Trying to get touch point, returns NaN试图获取接触点,返回 NaN
【发布时间】:2011-12-03 12:14:35
【问题描述】:

好的,我在这里遇到了一个非常奇怪的(对我而言)问题。我的视图中有一个 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

任何解决此问题的帮助将不胜感激!

【问题讨论】:

    标签: iphone objective-c location touch uigesturerecognizer


    【解决方案1】:

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

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

    我不知道内部细节,但如果它失败/取消它可能不会在视图中获取位置。

    这里是关于子类化手势识别器和可能发生的状态的文档。

    http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIGestureRecognizer_Class/Reference/Reference.html

    【讨论】:

    • 好的,谢谢你,但不知何故我的问题与滚动视图有关。获得触摸点的目的是以不同的方式为按钮设置动画,具体取决于您触摸它的位置。只是为了测试,我删除了检查触摸位置的部分,并在按下按钮时使按钮沿 y 轴向下移动,并在不再按下时将其移回其原始位置。在常规视图上它按预期工作,但在滚动视图上它仅在延迟后工作(如上所述)并且动画不流畅。有什么想法吗?
    【解决方案2】:

    我试图重现您的问题,但没有成功。起初,我认为手势识别器可能会干扰 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 方法对我有所帮助,希望这段代码对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 1970-01-01
      • 2019-06-26
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      相关资源
      最近更新 更多