【发布时间】:2010-10-28 12:28:04
【问题描述】:
我正在开发 iPad 应用程序,我必须在其中通过触摸将箭头旋转一圈。但我在这方面遇到了问题。问题在于图像必须旋转到的角度计算。
您可以查看here。 我必须围绕圆圈旋转大红色箭头图像。任何人都可以帮助我,如何获得触摸点的角度。目前我正在使用以下代码,我在网络的某个地方找到了它。但它并没有将箭头旋转到触摸的地方。
- (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;
}
谁能帮我正确计算角度。
谢谢
【问题讨论】:
-
使用具有 360 度平铺的一张图像。 Apple 也是这样做的,它提供了更大的灵活性和更好的质量,并且更容易编程。
-
针是移动到错误的地方,还是根本不动?
-
它正在移动到错误的位置。意味着针不指向我点击的地方,它指向任何其他地方。
-
科宁巴尔德!你能告诉我更详细的吗。你是什么意思以及如何实现这一点。谢谢
标签: iphone objective-c image-rotation