如何通过手指触摸自动围绕中心点旋转图像 [英] How to rotate image around center point automatically with finger touch

查看:38
本文介绍了如何通过手指触摸自动围绕中心点旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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

推荐答案

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

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
}

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

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

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

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! :)

这篇关于如何通过手指触摸自动围绕中心点旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆