以任何angular度刷卡检测

有什么办法可以检测iPhone在任何angular度刷卡? UISwipeGestureRecognizer似乎只有4个方向。
如果我这样刷卡:

 \ \ \ X 

我希望它给我60度的东西,而不是像UISwipeGestureRecognizer
我怎样才能做到这一点?

您可以使用UIPanGestureRecognizer 。 当你检测到结束状态时,你可以得到速度。 速度被分解成x和y分量。 您可以使用x和y分量来计算斜率m。

m =Δy/Δx

由斜率m定义的相对于x轴的线的angular度Σ定义为:

Σ= arctan(m)

就像是:

 - (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; } } 

您只需检测触摸的开始和停止,并使用这两个点计算angular度。

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //global CGPoint. //this should be it's 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]; } 

要计算它们之间的angular度,你可以使用这样的东西:

 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. 

所以要find一个水平线的angular度,你可以做这样的事情

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

那将是这样的angular度

 ________ \ this angle \ \ x 

希望这可以帮助

编辑一个更好的方法

你可以做的是子类UIGestureRecognizer

 #import <UIKit/UIGestureRecognizerSubclass.h> 

然后你实现这些方法

 - (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; 

其中每个都用于确定和设置手势的状态属性。

这里有一个完整的例子: http : //developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html