3

我试图vector从 a 中获取 a UIPanGestureRecognizer,关于屏幕的轴 in Portrait Orientation。我知道我可以获得速度,这是战斗的一半,但理想情况下,我想要一种获得关于屏幕垂直轴的角度(罗盘度或弧度)的方法。例如,从左下角到右上角的拖动是 45 度角,从上到下是 180 度,或者从下到上是 0(或 360)。

这可能吗?它没有方向属性,文档中对翻译的解释有点混乱。我是否需要手动创建一个中心点(关于我的视图中心),并将起点/终点与该点进行比较Pan Touch?我有点不确定需要的数学。

提前致谢!^_^

4

2 回答 2

6

如果你能得到速度:

CGPoint velocity = [recognizer velocityInView:self.view]

然后您可以计算相对于 x 轴的角度:

float x = velocity.x;
float y = velocity.y;

double angle = atan2(y, x) * 180.0f / 3.14159f;
if (angle < 0) angle += 360.0f; 
于 2013-01-14T13:14:49.690 回答
0
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);
}
于 2015-03-11T11:07:20.010 回答