Restricting UIPanGestureRecognizer movement
Asked Answered
H

3

2

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.

Higley answered 7/2, 2012 at 1:44 Comment(0)
C
7

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;
}
Cureall answered 7/2, 2012 at 5:13 Comment(2)
That worked, thanks. I made yMin an int, which required self->yMin instead. Was this the best way of going about things?Higley
Sounds like a fine way to do it. You can have a property that is type int. You just don't declare it retain/assign/strong/weak. But if you don't need to expose it as a property, you can just use an ivar. You can access the ivar by saying yMin. You don't have to say self->yMin unless you have a local variable also named yMin that you don't want to access.Cureall
K
2

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

Kluge answered 22/11, 2012 at 8:34 Comment(0)
C
0

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;
Chequered answered 7/2, 2012 at 4:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.