Ios 是否使用UILongPress手势识别器检测左右移动?

Ios 是否使用UILongPress手势识别器检测左右移动?,ios,objective-c,uigesturerecognizer,Ios,Objective C,Uigesturerecognizer,从H2CO3回答的这个问题中我已经知道,您可以使用以下方法检测UIPangestureRecognitor中的左或右移动: CGPoint vel = [gesture velocityInView:self.view]; if (vel.x > 0) { // user dragged towards the right } else { // user dragged towards the left } 当用户输入uigesturecognizerst

从H2CO3回答的这个问题中我已经知道,您可以使用以下方法检测
UIPangestureRecognitor
中的左或右移动:

CGPoint vel = [gesture velocityInView:self.view];
if (vel.x > 0)
 {
     // user dragged towards the right
 }
 else
 {
     // user dragged towards the left
 }
当用户输入
uigesturecognizerstatechanged
状态时,我想通过使用
uigesturecognizerstatechanged
使用
uigesturecognizerstatechanged
来检测用户点击并按住类似于上述代码的按钮时的左或右移动,但似乎我不能简单地使用
velocityInView
来实现我的情况


有人能帮我吗?

首先将识别器的
允许移动设置为一个大值(默认为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
。如果用户抬起手指时只需要一个通知,请使用
UIgestureRecognitizerStateEnded

首先将识别器的
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
。如果用户抬起手指时只需要一个通知,请使用
UIgestureRecognitizerStateEnded