UIPangestureRecognizer

UIPangestureRecognizer

本文介绍了在 Xcode 中使用 UIPanGestureRecognizer 和速度来确定滑动方向过于敏感并且在方向之间快速切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

澄清:

我想要实现的是:滑动手势(向上、向下、向右和向左)在 WebView 上.当任何手势发生时,这就是它需要发生的事情:只要滑动开始,就必须开始一个动作(该动作实际上是加载一个 html 链接,该链接将网络摄像机向上、向下、向右、向左移动).html 链接使相机一直向右或向左或向上或向下移动.我有另一个 html 链接,加载时会告诉网络摄像机停止.

What I'm trying to achieve is this: the swiping gestures (up, down, right and left) are on a WebView. When any of the gestures occur this is what it needs to happen: as soon as the swipe begins, an action must begin (the action is actually loading a html link which moves an ip camera to either up,down,right,left). The html link makes the camera to go all the way to the right or left or up or down. I have another html link which when loaded will tell the ip camera to stop.

所以它需要发生的是当状态是 UIGestureRecognizerStateBegan 链接加载并不断移动相机直到用户没有触摸屏幕并且状态变为 UIGestureRecognizerStateEnded 并触发另一个 html 链接在 WebView 中运行哪个链接将停止相机移动.正如我们所说,我发布的最初代码是这样做的,但是滑动太敏感了.此问题已通过您的代码修复,但现在第二个链接在第一个链接之后立即加载,不允许相机移动.是否有意义??第一个链接需要运行,直到手指抬起.

So what it needs to happen is when the state is UIGestureRecognizerStateBegan the link load and moves the camera continuously until the user isn't touching the screen and the state becomes UIGestureRecognizerStateEnded and triggers the other html link to run in WebView which link will stop the camera from moving. As we speak my initial code posted does that but the swiping was too sensitive. This problem is fixed with your code but now the second link loads instantly after the first one, not allowing the camera to move. Does it make sense?? First link needs to run until the finger is lifted.

原始问题:

我试图在我管理的 UIPanGestureRecognizer 中使用速度确定滑动方向,它检测向右、向左、向上和向下滑动,它根据滑动方向触发适当的动作,并在 UIGestureRecognizerStateEnded 时再次触发正确的动作对于每次滑动,但是,我在下面的代码中的问题是方向非常敏感,例如在向右滑动期间,它会识别大部分向右滑动,但也会在两者之间向上或向下滑动.由于滑动并不总是完美的直线,我想找到的是仅在滑动大部分向右滑动时才触发向右滑动的动作并忽略微小的像素偏差.

I was trying to determine the swipe direction using velocity in a UIPanGestureRecognizer, which I managed and it detects the right, left, up and down swipes, it triggers the appropriate actions based on the swipe direction and again the correct actions when UIGestureRecognizerStateEnded for each swipe, BUT, my problem in the code below is that the direction is very sensitive for example during a swipe to the right it recognises most part of the swipe as to the right but also some swipe up or down in between. As the swipe is not always perfectly in a straight line, what I would like to find is to only trigger the action for swipe right if the swipe is MOSTLY to the right and ignore the tiny pixel deviations.

另外,对于我的项目来说,理想情况下是在给定的滑动方向上触发一次动作,就像在 UISwipeGestureRecognizer 中一样,但是使用它我对 UIGestureRecognizerStateEnded 有问题,因为它不会让滑动动作运行,直到我抬起我的手指离开屏幕,即使我的手指仍在滑动,它也会立即完成滑动操作.

Also, ideally for my project is to trigger the action on a given swipe direction only once just like in the UISwipeGestureRecognizer, but using it I have a problem with UIGestureRecognizerStateEnded as it doesn't let the swipe action run until I lift my finger off the screen, it finishes the swipe action right away even though my finger is still swiping.

任何帮助将不胜感激.这是我的代码:

Any help would be much appreciated. Here is my code:

在 ViewDidLoad 我有:

in ViewDidLoad I have:

UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[_myWebView addGestureRecognizer:gestureRecognizer];
[gestureRecognizer setMinimumNumberOfTouches:1];
[gestureRecognizer setMaximumNumberOfTouches:1];

在我的课堂上,我有:

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

if(velocity.x > 0)//right
{
   //my action here

  if ( [gestureRecognizer state] == UIGestureRecognizerStateEnded )
  { //my action when swipe finished here }
}
else if(velocity.x < 0)//left
{
   //my action here

  if ( [gestureRecognizer state] == UIGestureRecognizerStateEnded )
  { //my action when swipe finished here }
}
else if(velocity.y > 0)//down
{
   //my action here

  if ( [gestureRecognizer state] == UIGestureRecognizerStateEnded )
  { //my action when swipe finished here }
}
else if(velocity.y < 0)//up
{
   //my action here

  if ( [gestureRecognizer state] == UIGestureRecognizerStateEnded )
  { //my action when swipe finished here }
}

谢谢,任何澄清请询问.

Thank you, any clarifications please ask.

推荐答案

如果 x 组件是 y 组件.所以,也许如果 xy 的五倍,那可以被认为是水平滑动.反之亦然.5 是否是正确的倍数取决于您(使用 tan,您可以看到从绝对水平/垂直转换为大约 11.3°),但从概念上讲,这是一种轻松解决它的方法.

You could have some "horizontal" direction detection if the x component was some reasonable multiple of the y component. So, perhaps if x was five times y, that could be considered horizontal swipe. And vice versa for vertical. Whether five is the right multiple is up to you (using tan, you can see that translates to roughly 11.3° from absolute horizontal/vertical), but it's conceptually one way to tackle it easily.

例如,这是一个手势识别器,当用户开始向特定方向滑动时,它会向相机发送一个命令以开始向该方向移动,并在用户将手指从屏幕上抬起时告诉相机停止:

For example, here's a gesture recognizer that will send a camera a command to initiate movement in a particular direction when the user starts a swipe in that direction, and will tell the camera to stop when the user lifts their finger off the screen:

CGFloat const gestureMinimumTranslation = 20.0;

typedef enum : NSInteger {
    kCameraMoveDirectionNone,
    kCameraMoveDirectionUp,
    kCameraMoveDirectionDown,
    kCameraMoveDirectionRight,
    kCameraMoveDirectionLeft
} CameraMoveDirection;

@interface ViewController ()
{
    CameraMoveDirection direction;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    [self.viewWithGestureRecognizer addGestureRecognizer:recognizer];
}

// This is my gesture recognizer handler, which detects movement in a particular
// direction, conceptually tells a camera to start moving in that direction
// and when the user lifts their finger off the screen, tells the camera to stop.

- (void)handleSwipe:(UIPanGestureRecognizer *)gesture
{
    CGPoint translation = [gesture translationInView:self.view];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        direction = kCameraMoveDirectionNone;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged && direction == kCameraMoveDirectionNone)
    {
        direction = [self determineCameraDirectionIfNeeded:translation];

        // ok, now initiate movement in the direction indicated by the user's gesture

        switch (direction) {
            case kCameraMoveDirectionDown:
                NSLog(@"Start moving down");
                break;

            case kCameraMoveDirectionUp:
                NSLog(@"Start moving up");
                break;

            case kCameraMoveDirectionRight:
                NSLog(@"Start moving right");
                break;

            case kCameraMoveDirectionLeft:
                NSLog(@"Start moving left");
                break;

            default:
                break;
        }
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // now tell the camera to stop
        NSLog(@"Stop");
    }
}

// This method will determine whether the direction of the user's swipe

- (CameraMoveDirection)determineCameraDirectionIfNeeded:(CGPoint)translation
{
    if (direction != kCameraMoveDirectionNone)
        return direction;

    // determine if horizontal swipe only if you meet some minimum velocity

    if (fabs(translation.x) > gestureMinimumTranslation)
    {
        BOOL gestureHorizontal = NO;

        if (translation.y == 0.0)
            gestureHorizontal = YES;
        else
            gestureHorizontal = (fabs(translation.x / translation.y) > 5.0);

        if (gestureHorizontal)
        {
            if (translation.x > 0.0)
                return kCameraMoveDirectionRight;
            else
                return kCameraMoveDirectionLeft;
        }
    }
    // determine if vertical swipe only if you meet some minimum velocity

    else if (fabs(translation.y) > gestureMinimumTranslation)
    {
        BOOL gestureVertical = NO;

        if (translation.x == 0.0)
            gestureVertical = YES;
        else
            gestureVertical = (fabs(translation.y / translation.x) > 5.0);

        if (gestureVertical)
        {
            if (translation.y > 0.0)
                return kCameraMoveDirectionDown;
            else
                return kCameraMoveDirectionUp;
        }
    }

    return direction;
}

@end

这演示了如何确定平移手势的初始方向,然后在初始方向确定后以及手势结束时采取行动.

This demonstrates how you could determine the initial direction of a pan gesture and then act once the initial direction is established, as well as upon the end of the gesture.

这篇关于在 Xcode 中使用 UIPanGestureRecognizer 和速度来确定滑动方向过于敏感并且在方向之间快速切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 03:11