我正在开发 iPad 应用程序,我必须在其中通过触摸将箭头旋转一圈。但我在这方面遇到了问题。问题在于图像必须旋转到的角度计算。
你可以在这里查看。我必须围绕圆圈旋转大红色箭头图像。任何人都可以帮助我,如何获得触摸点的角度。目前我正在使用以下代码,我在网络的某个地方找到了它。但它并没有将箭头旋转到触摸的地方。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *oneTouch = [touches anyObject];
CGPoint currentPoint = [oneTouch locationInView:imgCompass];
double current_angle = [self wheelAngleFromPoint:currentPoint];
imgNeedle.transform = CGAffineTransformMakeRotation( current_angle );
}
- (double) wheelAngleFromPoint:(CGPoint)location
{
double retAngle;
// subtract center of wheel
location.x -= (self.imgNeedle.bounds.size.width ) / 2.0;
location.y = (self.imgNeedle.bounds.size.height ) / 2.0 - location.y;
// normalize vector
double vector_length = sqrt(location.x*location.x + location.y*location.y);
location.x = location.x/vector_length;
location.y = location.y/vector_length;
retAngle = acos(location.y);
float offset = 0.28;
//if (location.x)
// offset = 0.28;
retAngle += offset;
if (location.x<0)
{
retAngle = -retAngle;
}
return retAngle;
}
谁能帮我正确计算角度。
谢谢