Search code examples
iosuigesturerecognizerimplementationgesturedirection

ios Implement UISwipeGestureRecogniser in any direction at any angle


I was trying to swipe things in left-down position(means between left and down). But UISwipeGesture recognises left,right,down and up only.

I want to add gesture on my whole screen view. Then, whenever user swipes on screen, I want to know the starting and end coordinates of swipe.That swipe could be at any angle and any position. Is there any way to get starting and ending coordinates of swipe in iOS

Please help me. I am badly stuck. Thanx in advance.


Solution

  • Now I did this implementation by using these steps:-

    You can implement some delegate methods after extending class "UIGestureRecognizer".

    Here are the methods.

    // It will give the starting point of touch

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
     CGPoint startSwipePoint= [touches.anyObject locationInView:self.view];
    }
    

    // It will give the point of touch on regural basis

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
     CGPoint point=[touches.anyObject locationInView:self.view];
    }
    

    //It will give the end swipe point

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {  
        CGPoint endSwipePoint= [touches.anyObject locationInView:self.view];
    }
    

    // Here is the code to calculate angle between two points. I am using start point and end point. But you can use any of the two point according to your requirement

    CGFloat angle=atan2(startSwipePoint.y - endSwipePoint.y, endSwipePoint.x - startSwipePoint.x) * Rad2Deg;