关于ios:从UIPanGestureRecognizer获取向量

Get a vector from a UIPanGestureRecognizer

我正在尝试从UIPanGestureRecognizer围绕Portrait Orientation中的屏幕轴获取vector。我知道我可以得到速度,这只是战斗的一半,但是理想情况下,我希望获得一种围绕屏幕垂直轴获取angular(罗盘度或弧度)的方法。例如,从左下到右上的拖动将是45度角,从上到下将是180度,或者从下到上将是0(或360)。

这可能吗?它没有Direction属性,并且文档中的翻译说明有些混乱。我是否需要手动创建一个中心点(关于视图的中心),并比较Pan Touch的起点/终点?我对所需的数学有点不

谢谢! ^ _ ^


如果可以使用:

来获取速度

1
CGPoint velocity = [recognizer velocityInView:self.view]

然后您可以使用以下方法计算相对于x轴的angular:

1
2
3
4
5
float x = velocity.x;
float y = velocity.y;

double angle = atan2(y, x) * 180.0f / 3.14159f;
if (angle < 0) angle += 360.0f;


1
2
3
4
5
6
7
8
9
10
11
12
13
CGPoint startLocation = [sender locationInView:self.view];
if (sender.state == UIGestureRecognizerStateBegan) {
     startLocation = [sender locationInView:self.view];
}
else if (sender.state == UIGestureRecognizerStateEnded) {
     CGPoint stopLocation = [sender locationInView:self.view];
     CGFloat dx = stopLocation.x - startLocation.x;
     CGFloat dy = stopLocation.y - startLocation.y;

     double angle = atan2(dy, dx) * 180.0f / M_PI;
     if (angle < 0) angle += 360.0f;
     NSLog(@"angle: %f",angle);
}