如何实现滑动和长按手势识别器?

3

我有两个手势识别器,分别是向右滑动手势和长按手势。我尝试使用代理方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:来同时识别这两个手势,但是当我进行滑动和长按手势时,该方法被调用了多次而不是一次。我使用以下代码设置手势识别器,调用代理方法并在它们执行后处理手势。

//Setting up the swipe gesture recognizer
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
swipeRight.delegate = self;
[self addGestureRecognizer:swipeRight];

//Setting up the long press gesture recognizer
UILongPressGestureRecognizer *rightLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
rightLongPressRecognizer.delegate = self;
rightLongPressRecognizer.tag = PRESS_RIGHT_TAG;
[rightLongPressRecognizer setMinimumPressDuration:0.5];
[self addGestureRecognizer:rightLongPressRecognizer];

//Delegate method
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;   
}

//Method that the gestures call 
-(void)handleSwipeRight: (UIGestureRecognizer *)recognizer {

    self.player.direction = RIGHT;
    [self resetSpriteView];
    [self.playerSprite startAnimating];

    float playerSpriteX = self.playerSprite.center.x;
    float playerSpriteY = self.playerSprite.center.y;
    self.toPoint = CGPointMake(playerSpriteX + TILE_WIDTH, playerSpriteY);
    if(!([self checkIfPlayerHasReachedEnd])) {
        self.fromPoint = CGPointMake(playerSpriteX, playerSpriteY);
        CABasicAnimation *moveAnimation = [CABasicAnimation animation];
        moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(playerSpriteX + TILE_WIDTH, playerSpriteY)];
        [moveAnimation setDelegate:self];
        [moveAnimation setFillMode:kCAFillModeForwards];
        [moveAnimation setRemovedOnCompletion:NO];
        [moveAnimation setDuration:MOVE_ANIMATION_DURATION];
        [self.playerSprite.layer addAnimation:moveAnimation forKey:@"position"];
    }
}

有没有更好的方法来实现滑动和长按手势识别器?


你为什么要使用相同的方法来处理这两种手势?如果你这样做,处理变得更加复杂是很正常的。 - Rui Peres
@JackyBoy - 我原以为必须为手势识别器设置一个方法才能使其工作,所以我不确定还有什么其他方法。有更好的方法吗? - pasawaya
是的,当手势被执行时,只需添加另一个选择器即可... - Rui Peres
1个回答

2

我认为这里的问题在于对委托模式的误解,以及当手势被执行时,方法的实际调用名称。没有别的。

将长按操作放在另一个方法中处理:

UILongPressGestureRecognizer *rightLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];

并在其自己的位置处理滑动手势:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];

你能详细解释一下吗? 我可以对我的代码做些什么来纠正我对代理的“误解”?不管怎样,感谢你的回答! - pasawaya
谢谢您的编辑,但在handleLongPress:handleSwipeRight中应该做什么?它们有相同的实现吗? - pasawaya
不,现在你要实现自己的逻辑。长按发生时会发生什么?向右滑动发生时会发生什么?这取决于你。 - Rui Peres
我明白。但是我需要在用户既滑动又长按时才触发某些操作。如果用户只长按或只滑动,我不希望有任何操作发生。 - pasawaya
这是一个不同的事情... 您应该在这里检查:-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer - Rui Peres
问题在于,当您知道两个手势同时完成时,您可以禁用它们...您需要执行必要的操作,然后重新启用它们... myGesture.enabled = NO; - Rui Peres

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接