Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NaN回答说,正在尝试接触点_Iphone_Objective C_Location_Touch_Uigesturerecognizer - Fatal编程技术网

Iphone NaN回答说,正在尝试接触点

Iphone NaN回答说,正在尝试接触点,iphone,objective-c,location,touch,uigesturerecognizer,Iphone,Objective C,Location,Touch,Uigesturerecognizer,好吧,我(对我来说)遇到了一个非常奇怪的问题。我的视图中有一个150x150按钮,我在该按钮上添加了一个ui长按手势识别器,因为我需要知道按下按钮时按钮被按下的位置。我的代码如下所示: -(CGPoint)detectedTouch:(UILongPressGestureRecognizer *)sender { CGPoint touchPoint = [sender locationInView:button]; return touchPoint; } -(void

好吧,我(对我来说)遇到了一个非常奇怪的问题。我的视图中有一个150x150按钮,我在该按钮上添加了一个
ui长按手势识别器
,因为我需要知道按下按钮时按钮被按下的位置。我的代码如下所示:

-(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
}
switch (sender.state){
    case  UIGestureRecognizerStatePossible:
        NSLog(@"possible");
        break;
    case  UIGestureRecognizerStateFailed:
        NSLog(@"Failed!");
        break;
    ... All Cases wanted
    default:
        break;
}
现在,当按钮处于正常视图时,一切正常。但是,当按钮位于滚动视图上时,它只在按下按钮约一秒钟后才起作用。如果你释放得太快,日志会告诉我:

touchPoint = nan, nan

如果您能帮助解决此问题,我们将不胜感激

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

-(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
}
switch (sender.state){
    case  UIGestureRecognizerStatePossible:
        NSLog(@"possible");
        break;
    case  UIGestureRecognizerStateFailed:
        NSLog(@"Failed!");
        break;
    ... All Cases wanted
    default:
        break;
}
我不知道内部细节,但如果失败/取消,它可能不会在视图中选择位置

下面是关于手势识别器子类化的文档以及可能发生的状态


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

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