0

在 iPhone 上,如何使用手指触摸实现围绕中心点的旋转图像?

就像滚轮一样,如果你把手指放在iPhone屏幕上,然后突然移动,然后图像像滚轮一样围绕中心点旋转,一段时间后,它变得越来越慢,最后停止。

谁能帮忙给出一些代码(Object-C)或一些建议?

4

3 回答 3

2

昨天我正在使用“旋转瓶子”应用程序。在窗口上,我有一个带有瓶子的 ImageView,它可以响应触摸并旋转用户滑动手指的方式。在触摸事件(TouchesBegan、Touchesoved、TouchesEnd)期间,我很难让我的 ImageView 旋转。我在 TouchesMoved 中使用了这段代码来找出女巫旋转图像的角度。

public override void TouchesMoved (NSSet touches, UIEvent evt)
{    
  PointF pt = (touches.AnyObject as UITouch).LocationInView(this);

  float x = pt.X  - this.Center.X;
  float y = pt.Y  - this.Center.Y;
  double ang =  Math.Atan2(x,y);

  // yada yada, rotate image using this.Transform
}

这个很重要!当 ImageView 旋转时,甚至 x 和 y 坐标也会发生变化。所以一直触摸同一个区域会给我在 pt 和 prePt 点上的不同值。经过一番思考、谷歌搜索和阅读,我想出了一个简单的解决方案。ImageView 的“SuperView”属性。

PointF pt = (touches.AnyObject as UITouch).LocationInView(this.SuperView);

有了这个小的改变让它变得容易多了,不,我可以使用 UITouch-metohs LocationInView 和 PreviousLocationInView 并获得正确的 x & y 坐标。她是我代码的一部分。

float deltaAngle;

public override void TouchesMoved (NSSet touches, UIEvent evt)
{
    PointF pt = (touches.AnyObject as UITouch).LocationInView(this.Superview);

    float x = pt.X  - this.Center.X;
    float y = pt.Y  - this.Center.Y;
    float ang =  float.Parse(Math.Atan2(dx,dy).ToString());


    //do the rotation
     if (deltaAngle == 0.0) {
       deltaAngle = ang;
     }
else
    {
      float angleDif = deltaAngle - ang;
      this.Transform = CGAffineTransform.MakeRotation(angleDif);
    }   
}

希望这可以帮助某人花费数小时来弄清楚如何旋转瓶子!:)

于 2011-08-02T09:42:49.463 回答
1

我会使用仿射变换——你可以使用 transform 属性将变换分配给任何层或 UI 元素。

您可以创建一个旋转变换,使用CGAffineTransform CGAffineTransformMakeRotation( CGFloat angle)它将返回一个旋转元素的变换。默认旋转应该围绕中心点。

请注意,旋转仅限于 360 度,因此如果您想旋转更多的东西(比如 720 度) - 您必须将旋转分成几个序列。

您可能会发现这篇 SO 文章也很有用。

于 2009-07-13T14:30:19.830 回答
1

视图或图层的变换属性可用于旋转其中显示的图像。就旋转部分而言,您只需使用 touchesBegan、touchesMoved 和 touchesEnded 在视图中跟踪触摸的位置和移动。

使用触摸更新之间的距离和时间来计算速度,并使用它来设置旋转速度。一旦开始图像旋转,请定期更新位置(可能使用 NSTimer),并将旋转速度降低一些常数。

于 2009-07-13T15:32:27.897 回答