2014-01-17 4 views
0

У меня есть UICollectionView, когда при нажатии ячейки будет создан новый набор данных и будет анимировать макет потока.UICollectionViewFlowLayout анимация между наборами данных

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
    for(int i=0;i<200;i++) 
    { 
     [tempArray addObject:[NSNumber numberWithInt:i]]; 
    } 

    items = [NSArray arrayWithArray:tempArray]; 

    CollapseLayout *currentLayout = (CollapseLayout *)_collectionView.collectionViewLayout; 
currentLayout.collapse = YES; 
[self.collectionView performBatchUpdates:^{ 
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.collectionView.numberOfSections)]]; 
} completion:nil]; 
} 

Он использует пользовательский UICollectionViewFlowLayout, который выполняет анимацию при перезагрузке данных. Мой CustomFlowLayout Класс здесь

#import "CollapseLayout.h" 

@implementation CollapseLayout 
- (void)prepareLayout { 
    self.itemSize = CGSizeMake(200, 100); 
    self.minimumInteritemSpacing = 30; 
    self.scrollDirection = UICollectionViewScrollDirectionVertical; 
} 

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect 
{ 
    NSArray* allAttributesInRect = [super layoutAttributesForElementsInRect:rect]; 
    if(_collapse) 
    { 
     for(UICollectionViewLayoutAttributes *attribute in allAttributesInRect) 
     { 
      attribute.frame = CGRectMake(attribute.frame.origin.x - self.collectionView.frame.size.width, attribute.frame.origin.y, attribute.frame.size.width, attribute.frame.size.height); 
     } 
    } 
    return allAttributesInRect; 
} 
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewLayoutAttributes* allAttributesInRect = [super layoutAttributesForItemAtIndexPath:indexPath]; 
    if(_collapse) 
    { 
     allAttributesInRect.frame = CGRectMake(allAttributesInRect.frame.origin.x - self.collectionView.frame.size.width, allAttributesInRect.frame.origin.y, allAttributesInRect.frame.size.width, allAttributesInRect.frame.size.height); 
    } 
    return allAttributesInRect; 
} 

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

- (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath*)itemIndexPath 
{ 
    UICollectionViewLayoutAttributes* layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:itemIndexPath]; 

    return layoutAttributes; 
} 

- (void)invalidateLayout 
{ 

} 

BOOL

_collapse 

устанавливается называется на мой взгляд, коллекция для запуска анимации. Я надеюсь, что старые ячейки, перемещение слева направо, вне представления, и новые ячейки ожидают с правой стороны экрана. Прямо сейчас, ячейки просто движутся с экрана.

Моя проблема заключается в том, что код в layoutAttributesForItemAtIndexPath и layoutAttributesForElementsInRect не анимируются правильно. Будет ли добавление кода анимации в эти методы лучшим местом для анимации, или им совершенно не нравится? Благодаря

ответ

1

Вы не должны делать ничего в результате

[super layoutAttributesForItemAtIndexPath:indexPath]; 

ни

[super layoutAttributesForElementsInRect:rect]; 

Если вы хотите, чтобы ваши клетки анимировать, откуда они должны покинуть экран, то вам нужно изменять только их «окончательную» позицию, переопределяя finalAttributesForDisappearingElementAtIndexPath:, но не их «обычную позицию».

Если вы хотите, чтобы они вернулись со стороны экрана, то вам необходимо изменить свою позицию через initialLayoutAttributesForAppearingItemAtIndexPath:

+0

ах, что это было! Благодаря! – Tony

+0

Добро пожаловать! –

Смежные вопросы