菜鸟教程小白 发表于 2022-12-12 09:43:55

ios - CollectionView 单元格滑动以执行操作


                                            <p><p>嗨,我正在使用 UICollectionview 水平布局开发社交网络应用程序,当我的应用程序启动 Collection View 时,最多会加载 5 个单元格,然后如果我滑动第 5 个单元格,那么那时只会分配第 6 个单元格内存。以同样的方式将第 6 个单元格滑动到第 7 个单元格。那么,我该如何实现这个过程</p>

<p>提前感谢您的任何建议</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要向您的 collectionView 添加滑动手势识别器:</p>

<pre><code>UISwipeGestureRecognizer *swipeGesture = [ initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
;
</code></pre>

<p>然后,在 handleSwipeGesture: 中处理滑动以分配单元格。</p>

<pre><code>-(void) handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
...
}
</code></pre>

<p>你可以随意调整滑动方向,这个是向上配置的。</p>

<p>这就是它的全部内容。主要是您不希望方向与滚动方向滑动冲突,因为我认为没有一种干净的方法来处理它。当您水平滚动时,即为滑动,因此您需要使用向上/向下滑动方向。</p>

<p><strong>改为附加到单元格</strong>(在单个单元格上捕获手势)我经常使用以下方法为简单的 Collection View 执行此操作:</p>

<pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    UILongPressGestureRecognizer *longPressGestureRecognizer = [ initWithTarget:self action:@selector(handleLongPress:)];
    ;
    ...
}
</code></pre>

<p>(这是一个长按,但它的工作方式相同)。在将其放入单元格的情况下,您需要在单元格上放置一个标签,然后在处理程序中引用该标签以找出它来自哪个单元格:</p>

<p>这是上面的一个:</p>

<pre><code>- (void) handleLongPress: (UILongPressGestureRecognizer *) sender
{
    if (sender.state != UIGestureRecognizerStateBegan)
      return;

    CGPoint p = ;
    NSIndexPath *indexPath = ;
    if (indexPath != nil)
    {
      UICollectionViewCell* cell = ;
      if (cell)
    {
    int indexOfItem = cell.tag;
      NSLog(@&#34;Selected item = %d&#34;, indexOfItem);
    }
}
</code></pre>

<p>至少沿着这条线...</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - CollectionView 单元格滑动以执行操作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23509121/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23509121/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - CollectionView 单元格滑动以执行操作