UICollectionView UseLayoutToLayoutNavigationTransi

2019-05-24 06:42发布

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.

2条回答
在下西门庆
2楼-- · 2019-05-24 06:45

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
查看更多
叛逆
3楼-- · 2019-05-24 06:54

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;
    }
}
查看更多
登录 后发表回答