在 Xcode 中使用 UIPanGestureRecognizer 和速度来确定滑动方向过于敏感并且在方向之间快速切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13873827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using UIPanGestureRecognizer in Xcode and velocity to determine swipe direction is too sensitive and switches rapidly between directions
提问by Dashony
Clarification:
澄清:
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.
我想要实现的是:滑动手势(向上、向下、向右和向左)在 WebView 上。当任何手势发生时,这就是它需要发生的事情:只要滑动开始,就必须开始一个动作(该动作实际上是加载一个 html 链接,该链接将网络摄像头向上、向下、向右、向左移动)。html 链接使相机一直向右或向左或向上或向下移动。我有另一个 html 链接,加载时会告诉网络摄像机停止。
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.
所以它需要发生的是当状态是 UIGestureRecognizerStateBegan 链接加载并不断移动相机直到用户没有触摸屏幕并且状态变为 UIGestureRecognizerStateEnded 并触发另一个 html 链接在 WebView 中运行该链接将停止相机从移动。正如我们所说,我发布的最初代码是这样做的,但滑动太敏感了。您的代码已修复此问题,但现在第二个链接在第一个链接之后立即加载,不允许相机移动。是否有意义??第一个链接需要运行,直到手指被抬起。
Original question:
原问题:
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.
我试图在我管理的 UIPanGestureRecognizer 中使用速度确定滑动方向,它检测向右、向左、向上和向下滑动,它会根据滑动方向触发适当的操作,并在每次滑动时触发 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.
此外,对于我的项目来说,理想情况下是只在给定的滑动方向上触发一次动作,就像在 UISwipeGestureRecognizer 中一样,但是使用它我对 UIGestureRecognizerStateEnded 有问题,因为它不会让滑动动作运行,直到我将手指从上面移开屏幕,即使我的手指仍在滑动,它也会立即完成滑动操作。
Any help would be much appreciated. Here is my code:
任何帮助将非常感激。这是我的代码:
in ViewDidLoad I have:
在 ViewDidLoad 我有:
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[_myWebView addGestureRecognizer:gestureRecognizer];
[gestureRecognizer setMinimumNumberOfTouches:1];
[gestureRecognizer setMaximumNumberOfTouches:1];
and in my class I have:
在我的课堂上,我有:
- (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.
谢谢,任何澄清请询问。
回答by Rob
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-1, you can see that translates to roughly 11.3° from absolute horizontal/vertical), but it's conceptually one way to tackle it easily.
如果x
组件是组件的合理倍数,您可以进行一些“水平”方向检测y
。所以,也许如果x
是五次y
,那可以被认为是水平滑动。反之亦然。五是否是正确的倍数取决于您(使用 tan -1,您可以看到从绝对水平/垂直转换为大约 11.3°),但从概念上讲,这是一种轻松解决它的方法。
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.
这演示了如何确定平移手势的初始方向,然后在建立初始方向后以及在手势结束时采取行动。
回答by konrad
First thing is you can use translation instead of velocity of the gesture - that'll give you the distance traveled from the beginning to the end of the swipe and not the direction of the movement at the very end of the swipe as with velocity.
首先,您可以使用平移而不是手势的速度 - 这将为您提供从滑动开始到结束的距离,而不是滑动结束时的移动方向,如速度。
Secondly, you probably should check if the gesture state is 'Ended' before checking the direction. Now you repeat the same check in each 'if'.
其次,您可能应该在检查方向之前检查手势状态是否为“结束”。现在您在每个“如果”中重复相同的检查。
To get the direction you can either get the angle of the translation using atan2 trigonometric function with x and y of the translation or if it's OK to always interpret the gesture as a swipe in one of 4 directions, simply find whether x or y has larger absolute value and then if it's positive or negative.
要获得方向,您可以使用 atan2 三角函数与平移的 x 和 y 获得平移的角度,或者如果始终将手势解释为 4 个方向之一的滑动,只需找出 x 或 y 是否更大绝对值,然后是正值还是负值。