在objC中将箭头UIImage旋转一圈
我正在开发 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;
}
谁能帮我计算正确的角度。
谢谢
I'm working on the iPad app, In which I have to rotate a arrow in a circle by touch. But I'm facing problems in it. The problem is in the angle calculation to which the image must be rotate.
You can check it here .
I have to rotate the big red arrow image around the circle. Can anybody help me, How to get the angle of the touched point. Currently I'm using the following code, I found it at somewhere in the net. But it didn't rotate the arrow to the touched place.
- (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;
}
Can anyone help me in correct angle calculation.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是这样的:
计算后
location.x
和location.y
中的值应该表示来自imgNeedle
中心的向量(或者轮?)到触摸点。请尝试这样做:请注意,由于您正在设置
imgNeedle.transform
属性,因此框架将无效,但中心将没问题:) - imgNeedle 将围绕图像的中心旋转。请注意,我明确指出了
location.x - self.imgNeedle.center.x
来帮助您了解发生了什么 - 您需要计算两个点之间的差异(它们之间的向量)。你应该明白,在数学中,(0, 0)为原点,x轴向右为正,y轴向上为正。但是,在设备屏幕上,您使用的坐标系(视图坐标)(0, 0) 位于左上角,x 轴向右为正,y 轴向下为正(这并不总是正确的,因为您可以通过多种方式映射屏幕坐标)。这就是为什么 location.y 减法与 location.x 减法相反 - 考虑到屏幕垂直“翻转”(就数学计算角度而言)。还需要知道的是,在数学中,正旋转表示逆时针方向旋转,并且在一个轴上翻转可以隐藏这一点,并使其看起来正角度意味着顺时针旋转(就像双负数使正),所以当你使用旋转和向量时,你应该考虑到所有这些。然而,就您的目的而言,它非常简单,因此您可能需要翻转其中一个方向(更改减法),甚至改变旋转方向(例如2*M_PI - retAngle
)。Here's the problem:
The values in
location.x
andlocation.y
after the calculation should represent a vector from the centre of theimgNeedle
(or wheel?) to the touch point. try this instead:Note that since you are setting the
imgNeedle.transform
property, the frame will be invalid, but the center will be ok :) - the imgNeedle will rotate about the center of the image.Notice that I explicitly stated
location.x - self.imgNeedle.center.x
to help you understand what's going on - you need to calculate the difference between the two points (a vector between them). You should understand that in mathematics, (0, 0) being the origin, the x-axis is positive to the right and the y-axis is positive upwards. However, on your device's screen, the co-ordinate system you are using (view co-ordinates) (0, 0) is at the top left with the x-axis going positive to the right but the y-axis going positive downwards (this isn't always true, because you can map your screen co-ordinates a number of ways). That is why the location.y subtraction is the other way around from the location.x subtraction - to account for the screen being "flipped" vertically (in terms of mathematically calculating the angle). It's also good to know that in mathematics, a positive rotation represents a rotation in the anti-clockwise direction, and a flip on one axis can hide this and make it appear that a positive angle means a clockwise rotation (like a double negative making a positive), so when you're playing with rotations and vectors, you should take all of this into account. However, for your purposes, it's quite simple, so instead of trying to wrap your head around all the maths you may just prefer to play with the code using trial and error, you might need to flip one of the directions (change the order of subtraction) or even change the direction of rotation (do2*M_PI - retAngle
for example).