简体   繁体   English

如何使UIPanGestureRecognizer的UILongPressGestureRecognizer失败?

[英]how to fail UILongPressGestureRecognizer for UIPanGestureRecognizer?

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]; 我用了[panRecognizer requireGestureRecognizerToFail:longPressRecognizer]; to fail long press recognizer. 使长按识别器失败。 But it doesnt happens. 但这并没有发生。 I also tried delegate method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: . 我还尝试了委托方法gestureRecognizer:应该同时使用GestureRecognizer:来识别 But it didnt work 但这没用

Try implementing delegate function 尝试实现委托功能

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

and return YES 然后返回是

Did you see this related answer Combining a UILongPressGestureRecognizer with a UIPanGestureRecognizer from @annie ? 您是否看到了这个相关答案, 将UILongPressGestureRecognizer与来自@annie 的UIPanGestureRecognizer组合在一起

You can do this solely with UILongPressGestureRecognizer . 您只能使用UILongPressGestureRecognizer进行此UILongPressGestureRecognizer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM