Domanda

I have a UIView object that can be dragged around using UIPanGestureRecognizer, but I only want it to be able to move up 3/4 of the screen. I don't want to it be clipped, but to get to a certain point and not be able to be dragged any further. What I have so far allows it to only move on the Y axis (which is desired).

- (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];
}

Thanks for any help.

È stato utile?

Soluzione

So just check whether the new Y coordinate would be too small. Don't change the view if it would be too small:

- (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;
}

Altri suggerimenti

Its work fine for me.

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

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

thanks,

Naveen Shan

Implement the following gesture delegate and check your condition inside it. Returning YES or NO from this delegate will make the gesture active and inactive.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top