开发者

how to rotate a UIView to face a point

I have a UIView that I 开发者_高级运维want to rotate when I move my finger across the screen (like in a circle). I want the UIView to rotate so that it faces my touchpoint. I then want to shoot something from my UIView (like a bullet) in the direction that the UIView is facing. Any Ideas???


Glad I remember 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;
}

Once you have your nice method, just do this:

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.

This is pretty much all the code that you'll need.The math is right, but I'm not sure about the setTransform stuff. I'm at school writing this in a browser. You should be able to figure it out from here.

Good luck,

Aurum Aquila

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜