UIPanGestureRecognizer限制坐标

时间:2012-07-11 20:13:30

标签: objective-c uiview uipangesturerecognizer

我在主要的UIView中添加了一个子视图(称为panel)并且我添加了gestureRecognizer,因为我希望它只能为Y轴拖动,并且仅限于某些限制(即160,300,超过300)它不能去。)

我用那种方式实现了手势处理

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view]; 
    recognizer.view.center = CGPointMake(self.view.frame.size.width/2, recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view.superview];

    //now limit the drag to some coordinates
   if (y == 300 || y == 190){
       no more drag
    }
}

但现在我不知道如何将阻力限制在那些坐标上。

这不是一个巨大的视图,它只是一个包含工具栏和按钮的小视图。

如何将拖动限制为坐标? (x = 160(中间屏幕),y = 404)< - 示例

中心应该在那里?

我google了很多,但我没有找到具体的答案。

提前致谢

4 个答案:

答案 0 :(得分:22)

首先,您需要在更改视图中心之前强制执行限制。在检查新中心是否超出范围之前,您的代码会更改视图的中心。

其次,您需要使用正确的C运算符来测试Y坐标。 =运算符是赋值。 ==运算符测试是否相等,但您也不想使用它。

第三,如果新中心超出范围,您可能不希望重置识别器的翻译。当拖动超出界限时重置平移将使用户的手指与他拖动的视图断开连接。

你可能想要这样的东西:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view];

    // Figure out where the user is trying to drag the view.
    CGPoint newCenter = CGPointMake(self.view.bounds.size.width / 2,
        recognizer.view.center.y + translation.y);

    // See if the new position is in bounds.
    if (newCenter.y >= 160 && newCenter.y <= 300) {
        recognizer.view.center = newCenter;
        [recognizer setTranslation:CGPointZero inView:self.view];
    }
}

答案 1 :(得分:12)

罗布的回答可能有一个意想不到的后果。如果拖得足够快,newCenter将超出界限,但可能会在上次更新之前发生。这将导致视图一直没有被淘汰到最后。如果newCenter超出界限,你应该总是更新中心但是限制边界,而不是不更新:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view];

    // Figure out where the user is trying to drag the view.
    CGPoint newCenter = CGPointMake(self.view.bounds.size.width / 2,
    recognizer.view.center.y + translation.y);

    // limit the bounds but always update the center
    newCenter.y = MAX(160, newCenter.y);
    newCenter.y = MIN(300, newCenter.y);

    recognizer.view.center = newCenter;
    [recognizer setTranslation:CGPointZero inView:self.view];
}

答案 2 :(得分:1)

以上两个答案对Y坐标都是正确的。 这是为谁寻找X&amp; Y坐标。刚做了一些小改动。

- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {
    UIView *piece = [panner view];
    CGPoint translation = [panner translationInView:[piece superview]];

    CGPoint newCenter = CGPointMake(panner.view.center.x + translation.x,
                                    panner.view.center.y + translation.y);
    if (newCenter.y >= 0 && newCenter.y <= 300 && newCenter.x >= 0 && newCenter.x <=300) 
    {
        panner.view.center = newCenter;
        [panner setTranslation:CGPointZero inView:[piece superview]];
    }
}

答案 3 :(得分:0)

-(void)handlePan:(UIPanGestureRecognizer*)pgr;
{
    if (pgr.state == UIGestureRecognizerStateChanged) {
        CGPoint center = pgr.view.center;
        CGPoint translation = [pgr translationInView:pgr.view];
        center = CGPointMake(center.x + translation.x,
                             center.y + translation.y);

        if ([self pointInside:center withEvent:nil])
        {
            pgr.view.center = center;
            [pgr setTranslation:CGPointZero inView:pgr.view];
        }
    }
}