我想弄清楚旋转一个UIView的数学某个边总是“跟随”一个手指,当在屏幕上移动。iOS数学炮塔/大炮旋转跟随一个手指
我几乎在那里,但我没有三角几年,我不知道该怎么做。这是我得到的:
-(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中有很多相似之处:http://www.raywenderlich.com/692/rotating-turrets – CodaFi 2012-03-23 22:47:53
你目前的解决方案与你想要发生的不同之处是什么? 'atan2'是用于即时追踪的最简单的东西,所以你看起来就像你已经弄清楚了。 – Tommy 2012-03-23 22:50:13
这并不完全正确。它在某些地方出现松动,当我保持X值不变并上下移动Y时,它会过度移动UIView。 – Derek 2012-03-23 22:56:48