1
-(void)move:(UIPanGestureRecognizer *)recognizer
{
    UIGestureRecognizerState state = [recognizer state];

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [recognizer translationInView:recognizer.view];
        [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
        [recognizer setTranslation:CGPointZero inView:recognizer.view];
    }
}
Bilal
  • 18,478
  • 8
  • 57
  • 72
Ashish
  • 706
  • 8
  • 22

1 Answers1

-1

There are several methods you can use;

Firstly, you can add a transparent subview in your main view and assign the pan gesture to that subview. Hence, only the pan gesture on subview will be recognized, and you can translate your main view directly.

Like this:

UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
    CGPoint translation = [recognizer translationInView:self.panView];

    [self.mainView setTransform:CGAffineTransformTranslate(self.mainView.transform, translation.x, translation.y)];

    [recognizer setTranslation:CGPointZero inView:self.panView];
}

PanGesture

(I didn't make panView completely transparent for show purposes, you should use clearColor instead of setting alpha to 0.)

(And obviously, panView is a subview of mainView, hence when the mainView is translated, panView also is translated.)

Secondly, you can customize your UIPanGestureRecognizer to implement touchesBegan: and raise a flag (like shouldRecognizeNow and check in move:method) if you don't want the touch to be recognized when started in particular place. (You may want to check CGRectContainsPoint: method.)

I'm not going into details of that, here's a link for starting:

https://stackoverflow.com/a/19145354/3227743

EDUsta
  • 1,932
  • 2
  • 21
  • 30
  • Thanks for answer but i add image on view and i add UIPanGestureRecognizer on image so i want to set the limit area that image not bound my view. – Ashish Jul 16 '17 at 17:59