14

내가 정의 UICollectionViewFlowLayout을 쓰고 있어요 그리고 내가 컬렉션보기에 performBatchUpdates:completion:를 호출 할 때 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]; 
}]; 

. 나는 의 레이아웃 속성을 바꾸고 있는지 확인하기위한 해결책을 생각해 냈다. 마지막 부분이지만, 그것은 해피하다고 느낀다.

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

다음, 나는 초기 레이아웃 항목과 장식 뷰 속성을 설정할 때 인덱스 경로의 섹션이 세트에 포함되어 있는지 확인합니다. nil 기본값이기 때문에

-(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를 반환하고 있습니다.

이렇게하면 더 멋진 회전 애니메이션을 추가 할 수있는 이점이 있습니다.

관련 문제