Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:32:26+00:00 2026-06-15T21:32:26+00:00

Clarification: What I’m trying to achieve is this: the swiping gestures (up, down, right

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:32:27+00:00Added an answer on June 15, 2026 at 9:32 pm

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For clarification, are you able to use MySQL this way to sort? ORDER BY
(Edited for clarification) My (non-OSGi) application build is in Gradle, and I am trying
This is a clarification question I've got from a Java EE 5 migration. I'm
Clarification: I somehow left out the key aspect: not using os.system or subprocess -
Looking for clarification on this... I've heard that 'everything' in .Net inherits from Object.
This is a clarification question: I'm studying for MCTS 70-515 and in my training
Clarification Upon working through your answers and reading your interpretations of this question, I
I just need clarification. I have this code below: if ((!$username) && (!$userid)) but
I am trying to get a little clarification to see if I am understanding
Clarification: this question is not about access modifier Confirmed that B.m() and b.m() statements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.