1

In my view, i added long press gesture and pan gesture as below

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(fitToView:)];
    [longPressRecognizer setDelegate:self];
    longPressRecognizer.allowableMovement = 5.0f;
    longPressRecognizer.minimumPressDuration = 2.0;
    [self addGestureRecognizer:longPressRecognizer];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    panRecognizer.delegate = self;
    [self addGestureRecognizer:panRecognizer];
    [panRecognizer requireGestureRecognizerToFail:longPressRecognizer];

and their gesture handlers are given below:

-(void)move:(UIPanGestureRecognizer*)recognizer {

    CGPoint translatedPoint = [recognizer translationInView:self.imgView ];

    if ([recognizer state] == UIGestureRecognizerStateBegan) {
        _firstX = [self.imgView center].x;
        _firstY = [self.imgView center].y;
    } 

    CGAffineTransform newTransform = CGAffineTransformTranslate(CGAffineTransformIdentity, translatedPoint.x , translatedPoint.y );

    self.imgView.transform = newTransform;

}

-(void)fitToView:(UILongPressGestureRecognizer*)recognizer {

    if ([recognizer state] == UIGestureRecognizerStateBegan) {
        self.imgView.transform = CGAffineTransformIdentity;
    }

}

long press is meant to restore the image. But when i moves the image long press gesture delegate also calls and restoring all the changes i have done. i used [panRecognizer requireGestureRecognizerToFail:longPressRecognizer]; to fail long press recognizer. But it doesnt happens. I also tried delegate method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:. But it didnt work

manujmv
  • 6,450
  • 1
  • 22
  • 35

2 Answers2

0

Try implementing delegate function

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

and return YES

Adithya
  • 4,545
  • 3
  • 25
  • 28
0

Did you see this related answer Combining a UILongPressGestureRecognizer with a UIPanGestureRecognizer from @annie?

You can do this solely with UILongPressGestureRecognizer.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179