如何旋转 UIView 面向一个点

发布于 2024-10-16 16:52:54 字数 114 浏览 0 评论 0原文

我有一个 UIView,当我在屏幕上移动手指(如画圈)时,我想旋转它。我希望 UIView 旋转,使其面向我的触摸点。然后我想从 UIView 朝 UIView 面向的方向射击一些东西(如子弹)。有什么想法吗???

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???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-10-23 16:52:54

很高兴我记得三角函数


-(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 的东西。我在学校用浏览器写这个。从这里你应该能够弄清楚。

祝你好运,

奥鲁姆·阿奎拉

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文