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.
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.
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.
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:
in ViewDidLoad I have:
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.