Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 710
vntstudy Avatar asked Aug 27 '13 11:08

vntstudy


2 Answers

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;
like image 105
vntstudy Avatar answered Sep 22 '22 05:09

vntstudy


Well, one route would be to just use a UIPanGestureRecognizer. Pan gestures don't care what direction their going, and are able to track their location within the view. Here's an example of how to get these points. (Ignore the obvious scope/storage issues)

- (void)panDetected:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        CGPoint startingPoint = [gesture locationInView:gesture.view];
    }else if (gesture.state == UIGestureRecognizerStateEnded) {
        CGPoint endPoint = [gesture locationInView:gesture.view];
    }
}

However, if you're dead set on using swipe gesture recognizers, you can modify a mostly complete diagonalish swipe gesture subclass that I made in the linked answer here: How to make a combined gestures?

This subclass allows you to specify a tolerance angle that you could set to 44.99 degrees which would allow swipe detection in virtually every direction.

like image 32
Mick MacCallum Avatar answered Sep 23 '22 05:09

Mick MacCallum