限制UIPanGestureRecognizer移动

时间:2012-02-07 01:44:34

标签: ios uiview uipangesturerecognizer

我有一个可以使用UIPanGestureRecognizer拖动的UIView对象,但我只希望它能够向上移动3/4的屏幕。我不希望它被剪裁,但是要达到某一点而不能被拖得更远。到目前为止,它只允许它在Y轴上移动(这是所需的)。

- (IBAction)panGesture:(UIPanGestureRecognizer *)recognizer
{
  CGPoint translation = [recognizer translationInView:self.view];
  recognizer.view.center = CGPointMake(recognizer.view.center.x, 
                                       recognizer.view.center.y + translation.y);
  [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:7)

所以只需检查新的Y坐标是否太小。如果视图太小,请不要更改视图:

- (IBAction)panGesture:(UIPanGestureRecognizer *)recognizer
{
  CGPoint translation = [recognizer translationInView:self.view];
  [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

  CGPoint center = recognizer.view.center;
  center.y += translation.y;
  if (center.y < self.yMin)
    return;
  recognizer.view.center = center;
}

答案 1 :(得分:2)

它对我来说很好。

  CGPoint currentTouchPoint = [gesture translationInView:self.bottomView];

    if (fabsf(currentTouchPoint.x) > fabsf(currentTouchPoint.y))    {
        // avoid horizontal movement of pan geuture.
        return;
    }

感谢,

Naveen Shan

答案 2 :(得分:0)

实施以下手势委托并检查其中的条件。从该代理返回YES或NO将使手势处于活动状态和非活动状态。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;