14

मैं एक कस्टम UICollectionViewFlowLayout लिख रहा हूँ और मैंने देखा है कि initialLayoutAttributesForAppearingItemAtIndexPath: और initialLayoutAttributesForAppearingDecorationElementOfKind:atIndexPath: जब मैं संग्रह दृश्य पर performBatchUpdates:completion: आह्वान सभी वर्गों के लिए बुलाया जाएगा। परिणाम यह है कि सभी अनुभागों में के बजाय एनीमेशन लागू किया गया है, जो कि नए जोड़े गए अनुभाग के बजाय है।UICollectionView performBatchUpdates: एनिमेट सभी वर्गों

[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]; 
}]; 

क्या मैं अब तक सरल अद्यतन के एवज में performBatchUpdates:completion: करने के लिए कॉल को हटा रहा है की कोशिश की है, लेकिन पहले से ही विद्यमान वर्गों (उन सभी को) किसी भी तरह एनिमेटेड रहे हैं। मैं यह सुनिश्चित करने के लिए जांच के समाधान के साथ आया हूं कि मैं पिछले अनुभाग में केवल के लेआउट विशेषताओं को बदल रहा हूं, लेकिन यह हैकी लगता है।

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

क्या यह केवल कुछ वर्गों को एनिमेट करने के बारे में जाने का उचित तरीका है?

उत्तर

7

ठीक है, मुझे जवाब मिला है; यह पिछले एक की तुलना में बहुत सुंदर नहीं है, लेकिन यह लेआउट को डेटासोर्स को छूने से रोकता है, इसलिए यह क्लीनर है।

असल में, हमें उन अनुभागों का ट्रैक रखने के लिए prepareForCollectionViewUpdates: और finalizeCollectionViewUpdates ओवरराइड करने की आवश्यकता है। मेरे पास एक म्यूटेबल सेट है जिसमें NSNumber अनुभाग शामिल हैं जो हम डालने वाले हैं।

-(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]; 
} 

इसके बाद, मैं देखने के लिए अगर सूचकांक पथ के अनुभाग है कि सेट में शामिल किया जाता है जब की स्थापना प्रारंभिक लेआउट वस्तुओं और सजावट विचारों के लिए जिम्मेदार बताते हैं की जाँच करें।

-(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; 
} 

मैं इन तरीकों अन्यथा से nil लौट रहा हूँ क्योंकि nil डिफ़ॉल्ट मान है।

इसमें निसार रोटेशन एनिमेशन होने का अतिरिक्त लाभ भी है।

संबंधित मुद्दे