2012-06-26 36 views
2

我有一個主視圖。 內部主視圖是兩個容器視圖:按鈕容器和顯示容器。 這些容器的內部分別是按鈕和顯示字段。所以簡而言之,我有三個層次的視圖:main,sub(容器)和sub-sub(按鈕和字段)。如何將視圖從容器視圖內移動到另一個容器視圖內?

當按下按鈕時,我想從該按鈕區域將該按鈕的圖像設置爲顯示區域的動畫。也就是說,我需要把它升到兩級,然後再降兩級。

目前,我在按鈕的頂部創建了一個UIImage,與自定義按鈕的UIImage相同。我移動它,然後在動畫結束時將其銷燬,所以我不必更改實際的按鈕(我想保留在原位以便重新使用它)。

顯然我可以得到這個UIImageView的中心/界限/框架。

但是,我無法確定目標的座標。框架和中心與超視圖相關,但這只是一個層面。似乎有很多數學手段來加總正確的X和Y偏移量到達目的地。

這是UIView的convertRect工作:toView:或convertRect:fromView:?我很難確定如何使用這些內容,或者決定它們是否實際上是正確的使用方法。

看起來像一個常見的問題 - 將某個「嵌套」視圖移動到另一個「嵌套」視圖 - 但我已經搜索並找不到答案。

+0

convertRect:...絕對是正確的方法。 – MusiGenesis

回答

0

那些convertRect方法很難得到。您的視圖包含兩個子視圖subA和subB,並且subA包含一個按鈕,並且您希望爲從subA移動到subB的按鈕設置動畫。讓我們在視圖控制器中執行具有主視圖的動畫....

// subA is the receiver. that's the coordinate system we care about to start 
CGRect startFrame = [subA convertRect:myButton.frame toView:self.view]; 

// this is the frame in terms of subB, where we want the button to land 
CGRect endFrameLocal = CGRectMake(10,10,70,30); 
// convert it, just like the start frame 
CGRect endFrame = [subB convertRect:endFrameLocal toView:self.view]; 

// this places the button in the identical location as a subview of the main view 
// changing the button's parent implicitly removes it from subA 
myButton.frame = startFrame; 
[self.view addSubview:myButton]; 

// now we can animate in the view controller's view coordinates 
[UIView animateWithDuration:1.0 animations:^{ 
    myButton.frame = endFrame; // this frame in terms of self.view 
} completion^(BOOL finished) { 
    myButton.frame = endFrameLocal; // this frame in terms of subB 
    [subB addSubview:myButton]; 
}]; 
+0

幾乎工作 - 不得不做一個改變:因爲subB不在容器視圖內,也就是它是視圖控制器視圖(self)的子視圖,所以我看不到需要convertRect處理endLocalFrame。由於subB的幀已經在主視圖的座標系中,只需從subB抓取幀並對其執行動畫即可 - 如** myButton.frame = subB.frame;} ** - 對嗎?這是一個很大的幫助,現在運行良好,謝謝。 – andrewmobile

+0

這將改變按鈕框架覆蓋subB。我以爲subB是一個更大的區域,你想要把它放在裏面。 – danh

相關問題