菜鸟教程小白 发表于 2022-12-12 20:13:16

ios - UICollectionView 单元格的水平重新排序


                                            <p><p>我有一个水平滚动的 UICollectionView。每个单元格对应于数组中的一个对象(货币代码)。我想要的是能够通过拖放手势重新排列单元格。</p>

<p>我找到了 <a href="https://www.raywenderlich.com/63089/cookbook-moving-table-view-cells-with-a-long-press-gesture" rel="noreferrer noopener nofollow">tutorial</a>对于 UITableView 并尝试过,但是当我按住并拖动一个单元格时,它只会垂直移动,当我将手指移动到屏幕边缘时不会滚动。这是 <a href="https://i.imgur.com/OzSP0dZ.gifv" rel="noreferrer noopener nofollow">gif</a> .</p>

<p>我想要发生的是让单元格水平移动,而不是垂直移动,并且当到达屏幕边缘时 Collection View 滚动。我将如何实现这一目标?</p>

<p>这就是我现在拥有的:</p>

<pre><code>UILongPressGestureRecognizer *longPress = [ initWithTarget: self action: @selector(longPressGestureRecognised:)];
;


-(IBAction) longPressGestureRecognised:(id)sender
{
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longPress.state;

CGPoint location = ;
NSIndexPath *indexPath = ;

static UIView *snapshot = nil;
static NSIndexPath *sourceIndexPath = nil;

switch (state) {
    case UIGestureRecognizerStateBegan: {
      if (indexPath) {
            sourceIndexPath = indexPath;

            UICollectionViewCell *cell = ;

            // Take a snapshot of the selected item using helper method.
            snapshot = ;

            // Add the snapshot as subview, centered at cell&#39;s centre.
            __block CGPoint centre = cell.center;
            snapshot.center = centre;
            snapshot.alpha = 0.0;
            ;
            [UIView animateWithDuration: 0.25 animations:^{

                // Offset for gesture location.
                centre.y = location.y;
                snapshot.center = centre;
                snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                snapshot.alpha = 0.98;

                // Fade out.
                cell.alpha = 0.0;

            } completion: ^(BOOL finished) {
                cell.hidden = YES;
            }];
      }
      break;
    }

    case UIGestureRecognizerStateChanged: {
      CGPoint centre = snapshot.center;
      centre.y = location.y;
      snapshot.center = centre;

      // Is destination valid and is it different from source?
      if (indexPath &amp;&amp; !) {
            // Update data source.
            ;

            // Move the items.
            ;

            // And update source so it is in sync with UI changes.
            sourceIndexPath = indexPath;
      }
      break;
    }

    default: {
      // Clean up.
      UICollectionViewCell *cell = ;
      cell.hidden = NO;
      cell.alpha = 0.0;
      [UIView animateWithDuration: 0.25 animations: ^{

            snapshot.center = cell.center;
            snapshot.transform = CGAffineTransformIdentity;
            snapshot.alpha = 0.0;

            // Undo fade out.
            cell.alpha = 1.0;

      }completion: ^(BOOL finished) {

            sourceIndexPath = nil;
            ;
            snapshot = nil;

      }];
      break;
    }
}
}


-(UIView *) customSnapshotFromView:(UIView *)inputView
{
// Make an image from the input view.
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
;
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Create an image view.
UIView *snapshot = [ initWithImage: image];
snapshot.layer.masksToBounds = NO;
snapshot.layer.cornerRadius = 0.0;
snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
snapshot.layer.shadowRadius = 5.0;
snapshot.layer.shadowOpacity = 0.4;

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

<p><strong>编辑:</strong>
我已经想通了。代码如下:</p>

<pre><code>UILongPressGestureRecognizer *longPress = [ initWithTarget: self action: @selector(handleLongGesture:)];
;

-(IBAction) handleLongGesture: (id)sender {
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;

CGPoint location = ;
NSIndexPath *indexPath = ;

switch (longPress.state) {
    case UIGestureRecognizerStateBegan:
      ;
      NSLog(@&#34;Gesture began&#34;);
      break;

    case UIGestureRecognizerStateChanged:
      ];
      NSLog(@&#34;Gesture state changed&#34;);
      break;

    case UIGestureRecognizerStateEnded:
      ;
      NSLog(@&#34;Gesture state ended&#34;);
      break;

    default:
      ;
      NSLog(@&#34;Gesture cancelled&#34;);
      break;
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在 UIGestureRecognizerStateChanged 中:
修改这行代码</p>

<pre><code>      CGPoint centre = snapshot.center;
      centre.x = location.x;
      snapshot.center = centre;
</code></pre>

<p>center.y 返回 y 轴拖动</p>

<p>将其更改为 center.x 以进行 x 轴拖动</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UICollectionView 单元格的水平重新排序,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37437614/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37437614/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UICollectionView 单元格的水平重新排序