2013-02-10 80 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