如何通过手指触摸自动围绕中心点旋转图像

发布于 2024-07-27 16:39:25 字数 154 浏览 4 评论 0原文

在iPhone上,如何通过手指触摸实现图像绕中心点旋转?

就像轮子一样,如果你把手指放在iPhone屏幕上,然后突然移动,那么图像就会像轮子一样围绕中心点旋转,过了一会儿,它变得越来越慢,最后停止。

谁可以帮忙提供一些代码(Object-C)或一些建议?

On iPhone, how to implement rotating image around the center point using finger touch ?

Just like wheel, if you put finger on the iPhone screen , then move suddenly, then the image becoming rotating around center point just like the wheel, after a while, it becomes more and more slow , finally stop.

Who can help to give some pieces of codes (Object-C) or some suggest ?

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

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

发布评论

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

评论(3

只为守护你 2024-08-03 16:39:25

昨天我正在使用“旋转瓶子”应用程序。 在窗口上,我有一个带有瓶子的 ImageView,它应该响应触摸并按照用户滑动手指的方式旋转。 我很难让 ImageView 在触摸事件(TouchesBegan、Touchesoved、TouchesEnd)期间旋转。 我在 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);
    }   
}

希望这可以帮助人们不再花几个小时研究如何旋转瓶子! :)

I was working with a "spin the bottle"-app yesterday. On the window I have a ImageView with an bottle that's suppose to response to touches and rotate the way the user swipes his finger. I struggled to get my ImageView to rotate during the touch-events (TouchesBegan, Touchesoved, TouchesEnd). I used this code in TouchesMoved to find out the angle in witch to rotate the image.

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
}

THIS IS IMPORTANT! When the ImageView rotates, even the x & y-coordinates changes. So touching the same area all the time would give me different values in the pt and prePt-points. After some thinking, googeling and reading I came up with an simple solution to the problem. The "SuperView"-property of the ImageView.

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

Having that small change in place made it alot easier, no i can use the UITouch-metohs LocationInView and PreviousLocationInView and get the right x & y coordinates. Her is parts of my code.

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);
    }   
}

Hope that helped someone from spending hours on how to figure out how to freaking rotate a bottle! :)

水溶 2024-08-03 16:39:25

我将使用仿射变换 - 您可以使用变换属性将变换分配给任何图层或 UI 元素。

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

请注意,旋转仅限于 360 度,因此如果您想要旋转更多的角度(例如 720 度) - 您必须将旋转分解为多个序列。

您可能会找到这篇SO文章 也很有用。

I would use the affine transformations - yuou can assign a transformation to any layer or UI element using the transform property.

You can create a rotation transform using CGAffineTransform CGAffineTransformMakeRotation( CGFloat angle) which will return a transformation that rotates an element. The default rotation should be around the centerpoint.

Be aware, the rotation is limited to 360 degrees, so if you want to rotate something more than that (say through 720 degrees) - you have to break the rotation into several sequences.

You may find this SO article useful as well.

迷荒 2024-08-03 16:39:25

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

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

The transform property of a view or layer can be used to rotate the image displayed within. As far as the spinning part goes, you just track the location and movement of touches in your view with touchesBegan, touchesMoved, and touchesEnded.

Use the distance and time between the touches updates to calculate a speed, and use that to set a rotational velocity. Once you start the image spinning, update the position periodically (with an NSTimer, maybe), and reduce the rotational velocity by some constant.

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