2011-02-06 62 views
1

我有一個UIView,當我在屏幕上移動我的手指(像一個圓圈)時,我想旋轉它。我想UIView旋轉,以便它面對我的接觸點。然後我想從UIView(如子彈)向UIView所面向的方向拍攝一些東西。有任何想法嗎???如何旋轉一個UIView來面對一個點

回答

2

很高興我記得triginometry


-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{ 

    float dX = touchPoint.x-objPos.x;  // distance along X 
    float dY = touchPoint.y-objPos.y;  // distance along Y 
    float radians = atan2(dY, dX);   // tan = opp/adj 

    //Now we have to convert radians to degrees: 
    float degrees = radians*M_PI/360; 

    return degrees; 
} 

一旦你有你的好方法,只是這樣做:

CGAffineTransform current = view.transform; 

[view setTransform:CGAffineTransformRotate(current,
[view degreesTorotateObjectWithPosition:view.frame.origin
andTouchPoint:[touch locationInView:parentView]] //Note: parentView = The view that your object to rotate is sitting in.

這是幾乎所有你會need.The數學代碼是正確的,但我不確定setTransform的東西。我在學校用瀏覽器寫這篇文章。你應該能夠從這裏弄清楚。

祝你好運,

奧羅拉奎拉

相關問題