【问题标题】:UIPanGestureRecognizer beyondBoundsUIPanGestureRecognizer BeyondBounds
【发布时间】:2012-11-05 20:03:08
【问题描述】:

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

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)。

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

谢谢

【问题讨论】:

    标签: objective-c ios frame uigesturerecognizer uipangesturerecognizer


    【解决方案1】:

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

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

    顺便说一句:

    您在使用UITapGestureRecognizer 的代码中谈论UIPanGestureRecognizer

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

    //...
    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];
    }
    

    【讨论】:

      【解决方案2】:

      您应该检查UIGestureRecognizer 中的numberOfTouches,例如:

      if (recognizer.numberOfTouches) {
            CGPoint tapPoint = [recognizer locationOfTouch:0 inView:self.view];
      }
      

      【讨论】:

      • 知道为什么如果 UIGestureRecognizer.state 开始,触摸次数会是 0 吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多