菜鸟教程小白 发表于 2022-12-11 17:49:00

ios - UICollectionView 移动项目并保持单元格大小


                                            <p><p>我有不同大小的单元格,我正在尝试移动 CollectionView 单元格,并保持移动/选定的单元格大小移动到新位置。</p>

<p>我已经制作了<a href="https://github.com/JCzz/ProfileToOverview" rel="noreferrer noopener nofollow">project</a>在 Github 上公开,如果您想被添加为合作者,请告诉我?</p>

<p>我是子类 <code>UICollectionViewFlowLayout</code> 并实现了以下内容:</p>

<pre><code>- (UICollectionViewLayoutInvalidationContext *)invalidationContextForInteractivelyMovingItems:(NSArray&lt;NSIndexPath *&gt; *)targetIndexPaths withTargetPosition:(CGPoint)targetPosition previousIndexPaths:(NSArray&lt;NSIndexPath *&gt; *)previousIndexPaths previousPosition:(CGPoint)previousPosition{

    UICollectionViewLayoutInvalidationContext *context = [super
                                                          invalidationContextForInteractivelyMovingItems:targetIndexPaths
                                                          withTargetPosition:targetPosition
                                                          previousIndexPaths:previousIndexPaths
                                                          previousPosition:previousPosition];

    toIndexPath:targetIndexPaths];

    return context;
}
</code></pre>

<p>也尝试在 UICollectionViewController 中实现这一点:</p>

<pre><code>-(NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath{
    if (originalIndexPath.section == proposedIndexPath.section) {
      returnproposedIndexPath;
    }else{
      return originalIndexPath;
    }
}
</code></pre>

<p>你知道如何保持尺寸吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我遇到了同样的问题,原因是您需要调用委托(delegate)的 moveItemAtIndexPath。 </p>

<pre><code>- (UICollectionViewLayoutInvalidationContext *)invalidationContextForInteractivelyMovingItems:(NSArray&lt;NSIndexPath *&gt; *)targetIndexPaths withTargetPosition:(CGPoint)targetPosition previousIndexPaths:(NSArray&lt;NSIndexPath *&gt; *)previousIndexPaths previousPosition:(CGPoint)previousPosition{

    UICollectionViewLayoutInvalidationContext *context = [super
                                                          invalidationContextForInteractivelyMovingItems:targetIndexPaths
                                                          withTargetPosition:targetPosition
                                                          previousIndexPaths:previousIndexPaths
                                                          previousPosition:previousPosition];

    toIndexPath:targetIndexPaths];

    return context;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UICollectionView 移动项目并保持单元格大小,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39977555/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39977555/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UICollectionView 移动项目并保持单元格大小