我正试图从vector
获取UIPanGestureRecognizer
,关于Portrait Orientation
中屏幕的轴。我知道我可以获得速度,这是战斗的一半,但我理想地想要获得关于屏幕垂直轴的角度(罗盘度或弧度)的方法。例如,从左下角到右上角的拖动将是45度角,从上到下将是180度,或从下到上将是0(或360)。
这可能吗?它没有方向属性,文档中的翻译解释有点令人困惑。我是否需要手动创建一个中心点(关于我视图的中心),并将Pan Touch
的起点/终点与之比较?我对所需的数学不太了解。
提前致谢! ^ _ ^
答案 0 :(得分:5)
如果你可以通过以下方式获得速度:
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;
答案 1 :(得分: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);
}