【问题标题】:how to rotate a UIView to face a point如何旋转 UIView 以面对一个点
【发布时间】:2011-02-06 21:46:19
【问题描述】:

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

【问题讨论】:

    标签: iphone xcode uiview rotation


    【解决方案1】:

    很高兴我记得三角函数

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

    祝你好运,

    天鹰座

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多