2016-08-17 133 views
1

檢查下面的圖像。我需要突出顯示聚焦的單元格,使其高於collectionView中的所有單元格。
我知道我必須改變聚焦細胞的zIndex。怎麼做?
我需要邏輯。我的代碼在objective-c如何更改UICollectionView zIndex?

這是爲tvOS,但iOS代碼也將工作在這裏我猜。

enter image description here

+0

你有這樣的示例代碼嗎? –

+0

我沒有。如果我沒有記錯的話,你只需要將你的下一個重點單元格的'zPosition'改成'didUpdateFocusInContext:'方法內的'zPosition'。還將previousFocused單元格的zPosition設置爲零。它應該工作。 – abhi1992

+0

這是一個UICollectionViewCell的方法嗎?如果是這樣,我已經添加了它,但沒有被調用。做更多的設置需要。 –

回答

7

您是否嘗試過設置cell.layer.zPosition的值?

+0

這將工作。但我們應該手動設置以前單元格的圖層的zPosition爲零,當前活動單元格的圖層的zPosition爲一。 – abhi1992

2

您需要創建自定義集合視圖的FlowLayout。根據集合視圖的滾動位置或可見矩形計算zIndex。示例類如下所示。

class CustomCollectionViewLayout : UICollectionViewFlowLayout { 

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
     let attributes = super.layoutAttributesForElementsInRect(rect) 
     attributes?.forEach { 
      $0.zIndex = 0 //Compute and set 
     } 
     return attributes 
    } 

    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { 
     let attribute = super.layoutAttributesForItemAtIndexPath(indexPath) 
     attribute?.zIndex = 0 //Compute and set 
     return attribute 
    } 
} 
5

嘗試手動施加layer.zPositionUICollectionViewCell子類的zIndexapplyLayoutAttributes:方法:

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes 
{ 
    [super applyLayoutAttributes:layoutAttributes]; 
    self.layer.zPosition = layoutAttributes.zIndex; 
} 
+0

如何在此方法中獲取當前選定的索引 –

-1

也許設置集合視圖的z位置爲-2,unhighlight爲-1,並突出至0