1

我創建了一個非常基本的UICollectionView與佈局過渡位置:https://github.com/aubrey/TestCollectionView如何修復UICollectionViewFlowLayout不將樣式應用於單元格?

這裏有我的問題的視頻:http://cl.ly/XHjZ

我的問題是我不知道在哪裏/如何應用我添加到單元格的陰影。每當我添加它時,它都不會正確應用於轉換後的單元格,並在轉換回來後掛起。

在我didSelectItemAtIndexPath方法我試圖在這裏將陰影(無濟於事):

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 

if (self.collectionView.collectionViewLayout == self.smallLayout) 
{ 
    [self.largeLayout invalidateLayout]; 
    [self.collectionView setCollectionViewLayout:self.largeLayout animated:YES]; 
    [self.collectionView setPagingEnabled:YES]; 
} 

else 
{ 
    [self.smallLayout invalidateLayout]; 
    [self.collectionView setCollectionViewLayout:self.smallLayout animated:YES]; 
    [self.collectionView setPagingEnabled:NO]; 

} 
} 

我還申請了影子在那裏我建立我的自定義單元格:

@implementation MyCell 

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 

    self.contentView.backgroundColor = [UIColor whiteColor]; 

    self.myNumber = [UILabel new]; 
    self.myNumber.text = @"Data Array Didn't Load"; 
    self.myNumber.frame = CGRectMake(20, 20, 100, 100); 
    [self.contentView addSubview:self.myNumber]; 

//  Shadow Setup 
     self.layer.masksToBounds = NO; 
     self.layer.shadowOpacity = 0.15f; 
     self.layer.shadowRadius = 1.4f; 
     self.layer.shadowOffset = CGSizeZero; 
     self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath; 

} 
return self; 
} 

回答

1

有趣的問題 - 陰影總是會引起問題,不是嗎?如果我理解正確,問題不在於影子沒有出現,而在於影子不在尊重細胞的新界限。

通常情況下,將像這樣的自定義屬性應用於單元格的最佳位置是覆蓋applyLayoutAttributes:。然而,在這種情況下,這將是棘手的。這是因爲,與應用屬於UIKit的隱式動畫屬性不同,陰影設置在單元格的CALayer上,這意味着要獲得陰影的動畫,您可能需要明確的CAAnimation

使用顯式動畫的問題在於無法在運行時確定動畫的持續時間。另外,假設你想從一個佈局轉換到另一個佈局,而不需要動畫。 UICollectionView API中沒有設施來處理這個問題。

你真的碰到了蘋果工程師可能沒有預見到的問題的交集。我不相信你有很多選擇。重寫applyLayoutAttributes:並擺弄一個明確的動畫可能會起作用,但有前面提到的限制。最好的辦法是創建一個代表陰影的可調整大小的UIImage,然後將UIImageView添加到單元格的視圖層次結構中,以便隨着單元格的增長和縮小,帶有陰影的圖像視圖也一樣。我知道,從代碼的角度來看,這不是一個令人滿意的答案,但它是最通用的答案,會導致最少的挫折。

相關問題