如何使用UIPanGestureRecognizer捕获正在平移的方向? [英] How can I capture which direction is being panned using UIPanGestureRecognizer?

查看:159
本文介绍了如何使用UIPanGestureRecognizer捕获正在平移的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我一直在四处寻找太阳下捕捉多点触控手势的所有选项,我终于完成了一圈,回到了UIPanGestureRecognizer。

Ok so I have been looking around at just about every option under the sun for capturing multi-touch gestures, and I have finally come full circle and am back at the UIPanGestureRecognizer.

我想要的功能非常简单。我已经设置了两个手指平移手势,我希望能够根据我移动的像素数量来移动一些图像。我已经完成了所有工作,但是我希望能够捕获平移手势是否已经反转。

The functionality I want is really quite simple. I have setup a two finger pan gesture, and I want to be able to shuffle through some images depending on how many pixels I move. I have all that worked out okay, but I want to be able to capture if the pan gesture is REVERSED.

是否有一种内置方式,我只是没有看到检测回到手势?我是否必须存储我的原始起点,然后跟踪终点,然后查看它们之后的移动位置,如果它小于初始终点,然后相应地反转?我可以看到它工作,但我希望有一个更优雅的解决方案!!

Is there a built in way that I'm just not seeing to detect going back on a gesture? Would I have to store my original starting point, then track the end point, then see where they move after that and se if its less than the initial ending point and then reverse accordingly? I can see that working, but I'm hoping there is a more elegant solution!!

谢谢

编辑:

以下是识别器设置为触发的方法。它有点像黑客,但它的工作原理:

Here is the method that the recognizer is set to fire. Its a bit of a hack, but it works:

-(void) throttle:(UIGestureRecognizer *) recognize{

throttleCounter ++;

if(throttleCounter == 6){
    throttleCounter = 0;
    [self nextPic:nil];
}

UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer *) recognize;
UIView *view = recognize.view;
if(panGesture.state == UIGestureRecognizerStateBegan){
    CGPoint translation = [panGesture translationInView:view.superview];
    NSLog(@"X: %f, Y:%f", translation.x, translation.y);
}else if(panGesture.state == UIGestureRecognizerStateEnded){
    CGPoint translation = [panGesture translationInView:view.superview];
            NSLog(@"X: %f, Y:%f", translation.x, translation.y);
}
  }

我刚刚到了我的位置开始尝试跟踪值之间的差异...尝试告诉他们正在平移的方式

I've just gotten to the point where I am going to start trying to track the differences between values...to try and tell which way they are panning

推荐答案

在UIPanGestureRecognizer上你可以使用 -velocityInView:获取手势被识别时手指的速度。

On UIPanGestureRecognizer you can use -velocityInView: to get the velocity of the fingers at the time that gesture was recognised.

如果你想在平底锅右边做一件事,一件例如,你可以做以下事情:

If you wanted to do one thing on a pan right and one thing on a pan left, for example, you could do something like:

- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint velocity = [gestureRecognizer velocityInView:yourView];

    if(velocity.x > 0)
    {
        NSLog(@"gesture went right");
    }
    else
    {
        NSLog(@"gesture went left");
    }
}

如果您真的想要检测逆转,就像在你想比较一个新的速度和旧的速度,看它是否只是在相反的方向 - 无论哪个方向 - 你可以做:

If you literally want to detect a reversal, as in you want to compare a new velocity to an old one and see if it is just in the opposite direction — whichever direction that may be — you could do:

// assuming lastGestureVelocity is a class variable...

- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint velocity = [gestureRecognizer velocityInView:yourView];

    if(velocity.x*lastGestureVelocity.x + velocity.y*lastGestureVelocity.y > 0)
    {
        NSLog(@"gesture went in the same direction");
    }
    else
    {
        NSLog(@"gesture went in the opposite direction");
    }

    lastGestureVelocity = velocity;
}

乘法和加法可能看起来有些奇怪。它实际上是一个点积,但请放心,如果手势在同一个方向,它将是一个正数,如果它们是完全正确的角度则下降到0,如果它们是相反的话则变为负数方向。

The multiply and add thing may look a little odd. It's actually a dot product, but rest assured it'll be a positive number if the gestures are in the same direction, going down to 0 if they're exactly at right angles and then becoming a negative number if they're in the opposite direction.

这篇关于如何使用UIPanGestureRecognizer捕获正在平移的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆