gpt4 book ai didi

ios - 如何实现滑动并按住手势识别器?

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:32 25 4
gpt4 key购买 nike

我有两个手势识别器,可以识别向右滑动的手势和长按手势。我尝试使用委托(delegate)方法 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 但每当我执行滑动和长按手势时,该方法都会被调用多次而不是一次。我使用以下代码设置手势识别器,调用委托(delegate)方法,并在手势执行后处理它们。

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

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

最佳答案

我认为这里的问题是错误地理解了委托(delegate)的作用,以及在执行手势时调用的方法实际上是什么,没有别的。

用另一种方法处理长按:

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

并自行处理滑动手势:

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

关于ios - 如何实现滑动并按住手势识别器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16112778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com