【发布时间】:2012-03-23 22:37:30
【问题描述】:
我试图找出旋转 UIView 的数学运算,当在屏幕上移动时,某一侧总是“跟随”手指。
我快到了,但我已经好几年没学过三角学了,我也不知道该怎么做。这是我所拥有的:
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
int dy = cannon.frame.origin.y - touchPoint.y;
int dx = cannon.frame.origin.x - touchPoint.x;
cannon.transform = CGAffineTransformMakeRotation(atan2(dy,dx) - 135);
}
【问题讨论】:
-
如果这有帮助,Ray Wenderlich 写了关于旋转炮塔的文章。它在 cocos2d 中,但我认为在普通的旧 objc 中有很多相似之处:raywenderlich.com/692/rotating-turrets
-
您当前的解决方案与您希望实现的解决方案有何不同?
atan2是用于即时跟踪的最简单的方法,因此您看起来几乎已经弄清楚了。 -
这不是完全正确的。它在某些地方显得很突出,当我保持 X 值不变并上下移动 Y 时,它会使 UIView 移动太多。
标签: objective-c ios ios5 uiview trigonometry