1

我有这段代码可以在平移对象时检索对象的坐标:

UITapGestureRecognizer *moveBuildingTap = [[UITapGestureRecognizer alloc]
                                           initWithTarget:self action:@selector(moveobject:)];

方法moveobject内容:

CGPoint tapPoint=[recognizer locationOfTouch:0 inView:self.view];

我用它来改变框架 - 移动它 - 使用这些坐标的图像视图。

但是,在拖动图像时 - 触发uipangesturerecognizer动作,我发现当我将它拖动到绝对底部时,我得到一个错误 - [UIPanGestureRecognizer locationOfTouch:inView:]: index (0) beyond bounds (0)。

如何解决此异常并防止用户拖过这一点?

谢谢

4

2 回答 2

3

moveobject:即使手势识别器的私有触摸数组似乎为空,您的方法也会被调用,这很奇怪。

无论如何,一般来说,如果您不在手势识别器中处理多点触控手势,我建议您使用[recognizer locationInView:]而不是locationOfTouch:inView:.

顺便提一句:

UIPanGestureRecognizer在使用UITapGestureRecognizer.

我建议处理拖动特定视图的代码如下所示:

//...
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[someView addGestureRecognizer:panGR];
//...

- (void)handlePan:(UIPanGestureRecognizer *)gr
{
    CGPoint translation = [gr translationInView:gr.view];
    gr.view.frame = CGRectOffset(gr.view.frame, translation.x, translation.y);
    [gr setTranslation:CGPointZero inView:gr.view];
}
于 2012-11-05T22:43:40.427 回答
3

你应该检查numberOfTouchesUIGestureRecognizer喜欢的:

if (recognizer.numberOfTouches) {
      CGPoint tapPoint = [recognizer locationOfTouch:0 inView:self.view];
}
于 2015-04-24T12:18:15.543 回答