1

在我看来,我添加了长按手势和平移手势,如下所示

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

他们的手势处理程序如下:

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

}

长按是为了恢复图像。但是当我移动图像时,长按手势代表也会调用并恢复我所做的所有更改。我曾经[panRecognizer requireGestureRecognizerToFail:longPressRecognizer];长按识别器失败。但它不会发生。我还尝试了委托方法 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:。但它没有工作

4

2 回答 2

0

尝试实现委托功能

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

并返回 YES

于 2013-06-10T12:54:46.473 回答
0

您是否看到过这个相关的答案Combining a UILongPressGestureRecognizer with a UIPanGestureRecognizer from @annie

您可以仅使用UILongPressGestureRecognizer.

于 2015-01-08T01:10:08.837 回答