Detection of wires at any angle

Is there any way to detect scrolling in the iPhone from any angle? UISwipeGestureRecognizer seems to have only 4 directions.
If I put it like this:

 \ \ \ X 

I want it to give me something like 60 degrees, and not just down like UISwipeGestureRecognizer .
How can i do this?

+4
source share
2 answers

You can use UIPanGestureRecognizer . When you discover the state of Ended, you can get speed. The speed is divided into components x and y. You can use the x and y components to calculate the slope of m.

m = Δy / Δx

The angle of the line, Σ, determined by the slope, m relative to the x axis, is determined as follows:

Σ = arctan (m)

Sort of:

 - (void)didPan:(UIPanGestureRecognizer*)recognizer { switch (recognizer.state) { case UIGestureRecognizerStateBegan: ... break; case UIGestureRecognizerStateEnded: CGPoint velocity = [recognizer velocityInView:[recognizer.view superview]]; // If needed: CGFloat slope = velocity.y / velocity.x; CGFloat angle = atan2f(velocity.y, velocity.x); ... break; } } 
+7
source

You can simply determine the start and stop of the touch and calculate the angle with two points.

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //global CGPoint. //this should be it GLOBAL coordinates, not just relative to the view startPoint=[[touches anyObject] locationInView:self.superview.superview]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ //global CGPoint endPoint=[[touches anyObject] locationInView:self.superview.superview]; } 

To calculate the angle between them, you can use something like this:

 static inline CGFloat angleBetweenLinesInRadians(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) { CGFloat a = line1End.x - line1Start.x; CGFloat b = line1End.y - line1Start.y; CGFloat c = line2End.x - line2Start.x; CGFloat d = line2End.y - line2Start.y; CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x); CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x); CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d)))); return (line2Slope > line1Slope) ? degs : -degs; } //This code came from someone else and I don't remember who to give credit to. 

So, to find the angle of the horizontal line, you can do something like this

 CGFloat angle=angleBetweenLinesInRadians(startPoint, endPoint, startPoint, CGPointMake(startPoint.x + 10, startPoint.y)); 

That would be such an angle

 ________ \ this angle \ \ x 

Hope this helps

CHANGE the best way

What you can do is subclass UIGestureRecognizer

 #import <UIKit/UIGestureRecognizerSubclass.h> 

Then you implement these methods

 - (void)reset; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 

Each of them is used to define and set the state property for gestures.

Here is a complete example: http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html

+3
source

All Articles