Keeping the contentOffset in a UICollectionView while rotating Interface Orientation

后端 未结 24 1357
野性不改
野性不改 2020-12-04 07:22

I\'m trying to handle interface orientation changes in a UICollectionViewController. What I\'m trying to achieve is, that I want to have the same contentOffset afte

24条回答
  •  执笔经年
    2020-12-04 08:07

    The "just snap" answer is the right approach and doesn't require extra smoothing with snapshot overlays IMO. However there's an issue which explains why some people see that the correct page isn't scrolled to in some cases. When calculating the page, you'd want to use the height and not the width. Why? Because the view geometry has already rotated by the time targetContentOffsetForProposedContentOffset is called, and so what was the width is now the height. Also rounding is more sensible than ceiling. So:

    - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
    {
        NSInteger page = round(proposedContentOffset.x / self.collectionView.bounds.size.height);
        return CGPointMake(page * self.collectionView.bounds.size.width, 0);
    }
    

提交回复
热议问题