8

預設,奇怪的消失項目時滾動

我有collectionViewFlowLayout子與

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { 
    return YES; 
    } 

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect { 
    NSArray *arr = [super layoutAttributesForElementsInRect:rect]; 
    BBLog(@"ARRA:%@", arr); 
    for (UICollectionViewLayoutAttributes *attr in arr) { 
     if (CGAffineTransformIsIdentity(attr.transform)) { 
      attr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI); 
     } 
    } 

    return arr; 
} 

的CollectionView旋轉倒掛與

self.collectionView.transform = CGAffineTransformMakeRotation((CGFloat)M_PI); 

滾動但是,即使強制使用本地collectionViewFlowLayout沒有子,一個git這個錯誤

問題

我有兩條消息和更多的聊天,但當滾動底部(正常情況下)消失第二項。

layoutAttributesForElementsInRect對於給定的矩形回報兩個indexPaths 0-0和0-1兩個屬性,但委託方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

稱爲僅供indexPath 0-0

這裏圖像

TopScroll enter image description here

UPDATE 所以我發現它爲什麼會發生 - 這行代碼

attr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI); 

看,如果刪除變換

enter image description here

回答

0

對不起,所有的,但我發現原因並解決。

這對設備不是模擬器只發生

看三個CGRects

iPhone 5S

(CGRect) oldFrame = (origin = (x = -0.000000000000056843418860808015, y = 0), size = (width = 320.00000000000006, height = 314.00000000000006)) 

iPhone 5C

(的CGRect)oldFrame =(產地=(x = 0,y = 0),size =(width = 320,height = 314))

模擬器英特爾酷睿

(CGRect) oldFrame = (origin = (x = 0, y = 0), size = (width = 320, height = 314)) 

要所有的人我運用旋轉變換通過

CGAffineTransformMakeRotation((CGFloat)M_PI) 

首先它是在iPhone 5S ARM蘋果A7 CPU

其次它是在iPhone 5C ARM Apple A6 CPU

第三它在模擬器上的英特爾酷睿iX處理器。

所以,我的解決辦法是:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { 
    UICollectionViewLayoutAttributes *retAttr = [super layoutAttributesForItemAtIndexPath:indexPath]; 

    if (CGAffineTransformIsIdentity(retAttr.transform)) { 
     retAttr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI); 
    } 

    CGRect oldFrame = retAttr.frame; 

    oldFrame.origin.x = retAttr.frame.origin.x > 0 ?: 0; 

    retAttr.frame = oldFrame; 

    return retAttr; 
} 
+0

swift的任何解決方案? – DeyaEldeen

+1

在swift中沒有測試,現在只在objc中編碼。但我認爲同樣的問題將會發生。 –

1

我不能完全肯定,但我認爲,當你繼承的UICollectionViewFlowLayout,你不應該直接修改屬性,但做一個屬性的副本,修改它並返回它。

頂部聲明的簡短說明: 您將有子類UICollectionViewFlowDelegate(UICollectionViewDelegateFlowLayout的母公司),比讓你自己的屬性和修改它們如你所願,但是這將需要實施更多的自定義邏輯。

另外看看是否在控制檯中收到任何錯誤或警告。

退房這個問題:Warning: UICollectionViewFlowLayout has cached frame mismatch for index path 'abc'

希望我至少是有點幫助。