14

Tôi đang viết UICollectionViewFlowLayout tùy chỉnh và tôi nhận thấy rằng initialLayoutAttributesForAppearingItemAtIndexPath:initialLayoutAttributesForAppearingDecorationElementOfKind:atIndexPath: sẽ được gọi cho tất cả các mục khi tôi gọi performBatchUpdates:completion: trên chế độ xem bộ sưu tập. Kết quả là tất cả các phần có hoạt ảnh được áp dụng cho chúng thay vì chỉ phần mới được thêm vào.UICollectionView performBatchUpdates: animates tất cả các phần

[collectionView performBatchUpdates:^{ 
    currentModelArrayIndex++; 
    [collectionView insertSections:[NSIndexSet indexSetWithIndex:currentModelArrayIndex]]; 
    [collectionView reloadSections:[NSIndexSet indexSetWithIndex:currentModelArrayIndex-1]]; 
} completion:^(BOOL finished) { 
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:currentModelArrayIndex] atScrollPosition:UICollectionViewScrollPositionTop animated:YES]; 
}]; 

Những gì tôi đã cố gắng cho đến nay là loại bỏ các cuộc gọi đến performBatchUpdates:completion: thay cho bản cập nhật đơn giản, nhưng các phần đã tồn tại (tất cả trong số họ) đều hoạt hình trong anyway. Tôi đã đưa ra một giải pháp kiểm tra để đảm bảo rằng tôi đang thay đổi thuộc tính bố cục của chỉ phần cuối cùng, nhưng nó cảm thấy bị hack.

if (decorationIndexPath.section == [(id<UICollectionViewDataSource>)self.collectionView.delegate numberOfSectionsInCollectionView:self.collectionView] - 1) 
{ 
    layoutAttributes.alpha = 0.0f; 
    layoutAttributes.transform3D = CATransform3DMakeTranslation(-CGRectGetWidth(layoutAttributes.frame), 0, 0); 
} 

Đây có phải là cách thích hợp để thực hiện hoạt ảnh chỉ một số phần không?

Trả lời

7

OK, tôi đã có câu trả lời; nó không đẹp hơn nhiều so với cái trước, nhưng nó giữ cách bố trí không chạm vào nguồn dữ liệu, vì vậy nó sạch hơn.

Về cơ bản, chúng tôi cần ghi đè prepareForCollectionViewUpdates:finalizeCollectionViewUpdates để theo dõi các phần mà chúng tôi đang chèn. Tôi có một bộ có thể thay đổi chứa NSNumber trường hợp của các phần mà chúng tôi đang chèn.

-(void)prepareForCollectionViewUpdates:(NSArray *)updateItems 
{ 
    [super prepareForCollectionViewUpdates:updateItems]; 

    [updateItems enumerateObjectsUsingBlock:^(UICollectionViewUpdateItem *updateItem, NSUInteger idx, BOOL *stop) { 
     if (updateItem.updateAction == UICollectionUpdateActionInsert) 
     { 
      [insertedSectionSet addObject:@(updateItem.indexPathAfterUpdate.section)]; 
     } 
    }]; 
} 

-(void)finalizeCollectionViewUpdates 
{ 
    [super finalizeCollectionViewUpdates]; 

    [insertedSectionSet removeAllObjects]; 
} 

Tiếp theo, tôi kiểm tra xem phần đường dẫn chỉ mục có được bao gồm trong bộ đó khi đặt thuộc tính bố cục ban đầu cho mục và chế độ xem trang trí hay không.

-(UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)decorationIndexPath 
{ 
    UICollectionViewLayoutAttributes *layoutAttributes; 

    if ([elementKind isEqualToString:AFCollectionViewFlowLayoutBackgroundDecoration]) 
    { 
     if ([insertedSectionSet containsObject:@(decorationIndexPath.section)]) 
     { 
      layoutAttributes = [self layoutAttributesForDecorationViewOfKind:elementKind atIndexPath:decorationIndexPath]; 
      layoutAttributes.alpha = 0.0f; 
      layoutAttributes.transform3D = CATransform3DMakeTranslation(-CGRectGetWidth(layoutAttributes.frame), 0, 0); 
     } 
    } 

    return layoutAttributes; 
} 

-(UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath 
{ 
    UICollectionViewLayoutAttributes *layoutAttributes; 

    if ([insertedSectionSet containsObject:@(itemIndexPath.section)]) 
    { 
     layoutAttributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath]; 
     layoutAttributes.transform3D = CATransform3DMakeTranslation([self collectionViewContentSize].width, 0, 0); 
    } 

    return layoutAttributes; 
} 

Tôi trả lại nil từ các phương thức này nếu không vì nil là giá trị mặc định.

Điều này có lợi ích bổ sung khi có hoạt ảnh xoay đẹp hơn.

Các vấn đề liên quan