2017-06-17 86 views
0

我想將另一個collectionview(cellContentCollectionView)中的矩形轉換爲等同於我的根視圖控制器中的矩形。在swift中使用convert(_:to :)方法

用於實現此目的的實例方法是convert(_:to :),但是我在設置根視圖控制器框架中UIView的框架時遇到問題。

這裏是我迄今爲止...

cellContentCollectionView?.convert((playerLayer?.frame)!, to: fullScreenPlayerView) 

cellContentCollectionView是的CollectionView,它的位於。這恰好是在collectionViewCell。

有什麼建議嗎?

+0

轉換方法返回一個新的CGRect,你必須使用它,'讓newFrame的= cellContentCollectionView .convert?((playerLayer .frame)!到:fullScreenPlayerView)' –

+0

什麼是第二個參數的點[到查看:UIView?]呢?我有點困惑 – Stefan

+0

是轉換的目標視圖,視圖是你需要把你的矩形,你需要從'cellContentCollectionView'轉換'playerLayer?.frame'到'fullScreenPlayerView'座標系 –

回答

0

您沒有解釋cellContentCollectionViewplayerLayer之間的確切關係,所以我將假設最差的:playerLayer不是cellContentCollectionView的子視圖。

我還懷疑playerLayer實際上並不是一個視圖,而是一個CALayer。沒關係,因爲在iOS上,視圖的frame始終是它的layer.frame,所以我們可以處理這些圖層。

圖層的frame位於其超級圖層的幾何圖形中。所以你必須要求它的超級玩家轉換它的frame。但是,如果圖層未被轉換,您可以改爲讓圖層轉換它自己的bounds

如果您打算使用生成的矩形作爲目標圖層的框架,則必須將其轉換爲目標圖層的超級圖層。您不能僅轉換爲目標圖層的幾何圖形,並設置目標圖層的bounds,因爲它不會更新目標圖層的position

因此我覺得要這樣寫:

if 
    let playerLayer = playerLayer, 
    let fullScreenPlayerSuper = fullScreenPlayerView.layer.superlayer 
{ 
    let frame = playerLayer.convert(playerLayer.bounds, to: fullScreenPlayerSuper) 
    fullScreenPlayerView.frame = frame 
} 
+0

這不會產生任何錯誤,但是該代碼產生的幀是0,0,0,0 – Stefan

+0

'print(playerLayer.bounds)'輸出是什麼? –

+0

另外playerLayer是位於AVPlayer和UIView之上的AVPlayerLayer – Stefan

0

這是我會怎麼做,假設playerLayerUIView

guard let playerLayer = playerLayer, superview = playerLayer.superview else { return } 

fullScreenPlayerView.convert(playerLayer.frame, from: superview) 
+0

playerLayer是一個AVPlayerLayer我引用它的原因是因爲我將需要它的視覺輸出無論如何。然而,它被掩蓋到一個UIView,所以我會嘗試與您的代碼一起使用UIView – Stefan

+0

您的代碼不會產生任何東西。就好像它生產零,但它沒有說出來。 – Stefan