5

我想知道是否有一种简单的方法来设置最小滑动长度,即用户需要滑动的像素长度,以便将手势识别为滑动。

我注意到正常的滑动可能非常无响应(例如,与在您的照片库中滑动照片相比)。

这是正常的方式,但我想减少所需的滑动长度:

    - (void)viewDidLoad 
    {


    // SWIPING GESTURES:

    UISwipeGestureRecognizer *swipeLeftRecognizer;
    swipeLeftRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundLeftSwipe:)];
    swipeLeftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;
    //swipeRecognizer.numberOfTouchesRequired=1;
    [self.view addGestureRecognizer:swipeLeftRecognizer];
    [swipeLeftRecognizer release];

    UISwipeGestureRecognizer *swipeRightRecognizer;
    swipeRightRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundRightSwipe:)];
    swipeRightRecognizer.direction=UISwipeGestureRecognizerDirectionRight;
    //swipeRecognizer.numberOfTouchesRequired=1;
    [self.view addGestureRecognizer:swipeRightRecognizer];
    [swipeRightRecognizer release];


    [super viewDidLoad];
}

#pragma mark -
#pragma mark Swipes

- (void)foundLeftSwipe:(UISwipeGestureRecognizer *)recognizer {
    // do something
}

- (void)foundRightSwipe:(UISwipeGestureRecognizer *)recognizer {
    // do something
    }

我记得有一种方法可以获取像素开始位置和结束位置,然后比较两者,但只是想知道是否有更简单的方法,即通过在我的代码中简单地定义所需的最小滑动长度的值这里。


编辑:

这就是我重新编码整个事情的方式:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];

    CGFloat deltaXX = (gestureStartPoint.x - currentPosition.x); // positive = left, negative = right
    CGFloat deltaYY = (gestureStartPoint.y - currentPosition.y); // positive = up, negative = down

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive

    if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
        if (deltaXX > 0) {
                label.text = @"Horizontal Left swipe detected";
                [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
            }
        else {
                label.text = @"Horizontal Right swipe detected";
                [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
            }

    }


    if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
        if (deltaYY > 0) {
            label.text = @"Vertical up swipe detected";
            [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
            }
        else {
            label.text = @"Vertical down swipe detected";
            [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
        }


    }



}
4

4 回答 4

6

我面临着完全相同的事情,我正在寻找解决方案。没有必要的距离之类的东西,UISwipeGestureRecognizer但这并不是您可以使用的全部。我最终使用了UIPanGestureRecognizer!它看起来像一个超级类的滑动手势识别器,它肯定会公开更多有用的数据。

此类中有一个可用的方法,称为translationInView:(UIView *)aView返回CGPoint,其中xy是整个平移手势的总距离!对我们的要求非常有用的东西!它通过正负值来尊重方向,意思是:y=+100 -> 向上滑动 100 点,x=+100从左向右滑动 100 点,等等...

我正在做的是检查用户是否已经平移了给定数量的点(我正在寻找类似向下滑动的东西,所以对我来说它是y>150)然后做我的事情......

这是一些代码摘录:

  -(void)viewDidLoad
    {
    UIPanGestureRecoginzer *panDown=[[UIPanGestureRecoginzer alloc] initWithTarget:self action:@selector(swipedDown:)];
     [self.view addGestureRecognizer:panDown];
    }
    .
    .
    .
    -(void)swipedDown:(UIPanGestureRecoginzer *)recognizer
    {
    CGPoint panned=[recognizer translationInView:self.view];
    if(panned.y>150){
    //swiped down for 150 points
        doSomething();
       }
    }

它还为您提供平底锅的速度:velocityInView:

注意:如果您想获得整体平移效果,您应该使用您的超级视图(大多数情况下为 self.view)。

希望这会有所帮助:UIPanGestureRecognize 类参考

于 2014-03-15T07:34:39.610 回答
5

请参阅此文档http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

您可以使用 touchesBegan、touchesMoved 和 touchesEnded 方法来查找滑动的开始/结束位置。通过找到开始和结束之间的增量,您可以获得滑动长度。

也许是这样的。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self.view];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint gestureEndPoint = [touch locationInView:self.view];   

    // compare gestureStartPoint and gestureEndPoint to determine swipe length
}
于 2011-04-14T19:04:15.423 回答
1

看看这个问题:UISwipeGestureRecognizer Swipe length

于 2011-04-14T19:02:11.703 回答
0

斯威夫特 3:

var gestureStartPoint: CGPoint = .zero

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first

    gestureStartPoint = touch!.location(in: self.view)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    let gestureEndPoint = touch!.location(in: self.view)

    let dist = distance(gestureStartPoint, gestureEndPoint)
    print(dist)
}

func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
    let xDist = a.x - b.x
    let yDist = a.y - b.y
    return CGFloat(sqrt((xDist * xDist) + (yDist * yDist)))
}
于 2017-06-17T01:05:19.197 回答