【问题标题】:Get angle of rotation after rotating a view旋转视图后获取旋转角度
【发布时间】:2013-02-10 05:21:24
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# xamarin.ios rotation cgaffinetransform


    【解决方案1】:

    不确定这些 xxxy 以及所有其他类似成员的确切含义,但我的 猜测* 是您将无法仅使用这些来追溯应用的转换值(这就像追溯 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 类中存储/公开其他转换,例如 scaletranslate,但我相信您会知道从这里去哪里.



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

    【讨论】:

    • 你的方法是可靠的,正是我现在要采用的方法。但是,如果有人对这个主题有任何启发,请随意!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2017-08-19
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    相关资源
    最近更新 更多