我想弄清楚旋轉一個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