旋转视图后获取旋转角度

时间:2013-02-10 05:21:24

标签: c# xamarin.ios rotation cgaffinetransform

假设我使用以下方法旋转视图:

            CGAffineTransform t = CGAffineTransform.MakeIdentity();
            t.Rotate (angle);
            CGAffineTransform transforms = t;
            view.Transform = transforms;

当我最初进行CGAffineTransform时,如何在不跟踪我放入角度变量的情况下获取此视图的当前旋转角度?它与view.transform.xx / view.transform.xy值有关吗?

1 个答案:

答案 0 :(得分:1)

不确定这些xxxy以及所有其他类似成员的确切含义,但 guess * 是您无法追溯应用的只使用这些值进行转换(就像回顾1+2+3+4一样只知道你开始使用1并最终得到10 - 我认为*

在这种情况下,我的建议是从CGAffineTransform派生并存储所需的值,但由于它是一个结构你不能这样做,所以在我看来你最好的选择是编写一个包装类,如这样:

class MyTransform
{
    //wrapped transform structure
    private CGAffineTransform transform;

    //stored info about rotation
    public float Rotation { get; private set; }

    public MyTransform()
    {
        transform = CGAffineTransform.MakeIdentity();
        Rotation = 0;
    }

    public void Rotate(float angle)
    {
        //rotate the actual transform
        transform.Rotate(angle);
        //store the info about rotation
        Rotation += angle;
    }

    //lets You expose the wrapped transform more conveniently
    public static implicit operator CGAffineTransform(MyTransform mt)
    {
        return mt.transform;
    }
}

现在,已定义的运算符允许您像这样使用此类:

//do Your stuff
MyTransform t = new MyTransform();
t.Rotate(angle);
view.Transform = t;
//get the rotation
float r = t.Rotation;

//unfortunately You won't be able to do this:
float r2 = view.Transform.Rotation;

正如您可以看到这种方法有其局限性,但您始终只能使用MyTransform的一个实例来应用各种转换并将该实例存储在某处(或者,可能是这种转换的集合)。

您可能还想在MyTransform课程中存储/展示其他转换,例如 scale translate ,但我相信您会知道去哪里从这里开始。



* 如果我错了,请随时纠正我

相关问题