如何旋转UIView面对一个点

时间:2011-02-06 21:46:20

标签: iphone xcode uiview rotation

我有一个UIView,当我在屏幕上移动手指时(例如在圆圈中)我想要旋转。我希望UIView旋转,使其面向我的接触点。然后我想从UIView面向的方向拍摄我的UIView(像子弹)。任何想法???

1 个答案:

答案 0 :(得分:2)

很高兴我记得三角法


-(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.

这几乎是你需要的所有代码。数学是正确的,但我不确定setTransform的东西。我在学校用浏览器写这个。你应该能够从这里弄明白。

祝你好运,

Aurum Aquila