gpt4 book ai didi

ios - 使用UILongPressGestureRecognizer检测左右移动吗?

转载 作者:行者123 更新时间:2023-12-01 17:41:34 25 4
gpt4 key购买 nike

从我已经通过H2CO3回答的Detecting the direction of PAN gesture in iOS问题中已经知道的信息,您可以使用以下命令检测UIPanGestureRecognizer中的左移或右移:

CGPoint vel = [gesture velocityInView:self.view];
if (vel.x > 0)
{
// user dragged towards the right
}
else
{
// user dragged towards the left
}

当用户进入 UILongPressGestureRecognizer状态时,我想通过使用 UIGestureRecognizerStateChanged按住与上面的代码类似的按钮来检测向左或向右移动,但是似乎我不能简单地使用 velocityInView使事情在我的情况下起作用。

有人可以帮助我吗?

最佳答案

首先将识别器的allowableMovement设置为一个较大的值(默认为10像素)。并使用以下代码

-(void)longPressed:(UILongPressGestureRecognizer*)g
{
if (g.state == UIGestureRecognizerStateBegan) {
_initial = [g locationInView:self.view]; // _initial is instance var of type CGPoint
}
else if (g.state == UIGestureRecognizerStateChanged)
{
CGPoint p = [g locationInView:self.view];
double dx = p.x - _initial.x;
if (dx > 0) {
NSLog(@"Finger moved to the right");
}
else {
NSLog(@"Finger moved to the left");
}
}
}

请注意, UILongPressGestureRecognizer是连续的,因此您将收到 UIGestureRecognizerStateChanged的倍数。如果您只想在用户放开手指时发出一个通知,请使用 UIGestureRecognizerStateEnded

关于ios - 使用UILongPressGestureRecognizer检测左右移动吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17439134/

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