catu
catu

Reputation: 898

UICollectionView UseLayoutToLayoutNavigationTransitions with different datasources

I have two UICollectionView objects, that both have different source and delegate. I would like to achieve a "Photos app'esque" look with the transition, using UseLayoutToLayoutNavigationTransitions.

It doesn't work though. When I call the UseLayoutToLayoutNavigationTransitions it changes the layout, but not the content.

First picture is first collection view. A series of categories and the people contained in them.

Second picture is what I'd like the animation to end up in. A series of people within a certain category.

Last picture is what happens right now. Categories just get rearranged.

First CollectionViewWhat I'd like the animation to end up withWhat it looks like now.

Upvotes: 2

Views: 2300

Answers (2)

Samball
Samball

Reputation: 441

Have a look at http://www.objc.io/issue-12/collectionview-animations.html in the

Transitions Between UICollectionViewController Instances

section.

it basically shows you that you'll have to change the datasource and delegate manually by implementation of the navigation controller delegate methods:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController isKindOfClass:[FJDetailViewController class]]) {
        FJDetailViewController *dvc = (FJDetailViewController*)viewController;
        dvc.collectionView.dataSource = dvc;
        dvc.collectionView.delegate = dvc;
        [dvc.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_selectedItem inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
    }
    else if (viewController == self){
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
    }
}

Upvotes: 3

Juan Carlos Méndez
Juan Carlos Méndez

Reputation: 1093

Your problem is that during the transition iOS will change the datasource. See my answer to this question How to use useLayoutToLayoutNavigationTransitions in UICollectionView?

You can use the same pattern described there:

  • use UseLayoutToLayoutNavigationTransitions to get the layout changes
  • observe when the transition is done
  • set the datasource to the one you need at that point

Upvotes: 2

Related Questions