2013-02-10 169 views
0

比方说,我用下面的方法旋转视图:获取旋转角度旋转视图后

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

我怎样才能得到这种看法的当前旋转角度没有保留什么我把角轨道当我最初做CGAffineTransform时变量?它与view.transform.xx/view.transform.xy值有关吗?

回答

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只有一个实例应用所有这样转换rts并将该实例存储在某处(或者可能是这种转换的集合)。

您可能还需要存储/揭露其他变换一样规模MyTransform类翻译,但我相信你会知道在哪里可以从这里走。



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

+0

你的方法是坚实的,正是我会去了。但是如果任何人有任何光线照射在这个问题上自由! – LampShade 2013-02-10 20:09:43