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

有没有更好的方法来实现滑动并按住手势识别器

4

1 回答 1

2

我认为这里的问题是对委托做什么的误解,以及调用什么方法,当手势被执行时,实际上是,没有别的。

用另一种方法处理长按:

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

并自行处理滑动手势:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
于 2013-04-19T20:43:15.267 回答