使用长按手势重新排序tableview中的单元格?

Mic*_*all 14 uitableview ios long-press

我希望能够使用longPress手势(不使用标准重新排序控件)重新排序tableview单元格.在识别出longPress后,我希望tableView基本上进入"编辑模式",然后重新排序,好像我正在使用Apple提供的重新排序控件一样.

有没有办法在不需要依赖第三方解决方案的情况下做到这一点?

提前致谢.

编辑:我最终使用了在接受的答案中的解决方案,并依赖于第三方解决方案.

bud*_*ino 19

Swift 3并没有第三方解决方案

首先,将这两个变量添加到您的类中:

var dragInitialIndexPath: IndexPath?
var dragCellSnapshot: UIView?
Run Code Online (Sandbox Code Playgroud)

然后添加UILongPressGestureRecognizer到您的tableView:

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(onLongPressGesture(sender:)))
longPress.minimumPressDuration = 0.2 // optional
tableView.addGestureRecognizer(longPress)
Run Code Online (Sandbox Code Playgroud)

处理UILongPressGestureRecognizer:

// MARK: cell reorder / long press

func onLongPressGesture(sender: UILongPressGestureRecognizer) {
  let locationInView = sender.location(in: tableView)
  let indexPath = tableView.indexPathForRow(at: locationInView)

  if sender.state == .began {
    if indexPath != nil {
      dragInitialIndexPath = indexPath
      let cell = tableView.cellForRow(at: indexPath!)
      dragCellSnapshot = snapshotOfCell(inputView: cell!)
      var center = cell?.center
      dragCellSnapshot?.center = center!
      dragCellSnapshot?.alpha = 0.0
      tableView.addSubview(dragCellSnapshot!)

      UIView.animate(withDuration: 0.25, animations: { () -> Void in
        center?.y = locationInView.y
        self.dragCellSnapshot?.center = center!
        self.dragCellSnapshot?.transform = (self.dragCellSnapshot?.transform.scaledBy(x: 1.05, y: 1.05))!
        self.dragCellSnapshot?.alpha = 0.99
        cell?.alpha = 0.0
      }, completion: { (finished) -> Void in
        if finished {
          cell?.isHidden = true
        }
      })
    }
  } else if sender.state == .changed && dragInitialIndexPath != nil {
    var center = dragCellSnapshot?.center
    center?.y = locationInView.y
    dragCellSnapshot?.center = center!

    // to lock dragging to same section add: "&& indexPath?.section == dragInitialIndexPath?.section" to the if below
    if indexPath != nil && indexPath != dragInitialIndexPath {
      // update your data model
      let dataToMove = data[dragInitialIndexPath!.row]
      data.remove(at: dragInitialIndexPath!.row)
      data.insert(dataToMove, at: indexPath!.row)

      tableView.moveRow(at: dragInitialIndexPath!, to: indexPath!)
      dragInitialIndexPath = indexPath
    }
  } else if sender.state == .ended && dragInitialIndexPath != nil {
    let cell = tableView.cellForRow(at: dragInitialIndexPath!)
    cell?.isHidden = false
    cell?.alpha = 0.0
    UIView.animate(withDuration: 0.25, animations: { () -> Void in
      self.dragCellSnapshot?.center = (cell?.center)!
      self.dragCellSnapshot?.transform = CGAffineTransform.identity
      self.dragCellSnapshot?.alpha = 0.0
      cell?.alpha = 1.0
    }, completion: { (finished) -> Void in
      if finished {
        self.dragInitialIndexPath = nil
        self.dragCellSnapshot?.removeFromSuperview()
        self.dragCellSnapshot = nil
      }
    })
  }
}

func snapshotOfCell(inputView: UIView) -> UIView {
  UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
  inputView.layer.render(in: UIGraphicsGetCurrentContext()!)
  let image = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()

  let cellSnapshot = UIImageView(image: image)
  cellSnapshot.layer.masksToBounds = false
  cellSnapshot.layer.cornerRadius = 0.0
  cellSnapshot.layer.shadowOffset = CGSize(width: -5.0, height: 0.0)
  cellSnapshot.layer.shadowRadius = 5.0
  cellSnapshot.layer.shadowOpacity = 0.4
  return cellSnapshot
}
Run Code Online (Sandbox Code Playgroud)


Gra*_*min 8

他们在iOS 11中添加了一种方法。

首先,启用拖动交互并设置拖放委托。

然后实现moveRowAt,就像使用重新排序控件正常移动单元格一样。

然后实现如下所示的拖放委托。

tableView.dragInteractionEnabled = true
tableView.dragDelegate = self
tableView.dropDelegate = self

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { }

extension TableView: UITableViewDragDelegate {
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        return [UIDragItem(itemProvider: NSItemProvider())]
    }
} 

extension TableView: UITableViewDropDelegate {
    func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {

        if session.localDragSession != nil { // Drag originated from the same app.
            return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
        }

        return UITableViewDropProposal(operation: .cancel, intent: .unspecified)
    }

    func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是正确、最快、最简单的解决方案。第一次就成功了。谢谢你展示这个。不需要第三方库或愚蠢的长代码。 (4认同)

bvo*_*ang 7

使用iOS SDK工具无法做到这一点,除非您想从头开始将自己的UITableView + Controller放在一起,这需要大量的工作.你提到不依赖第三方解决方案,但我的自定义UITableView类可以很好地处理这个问题.随意查看:

https://github.com/bvogelzang/BVReorderTableView


sam*_*han 2

所以本质上你想要像“清除”一样的行重新排序,对吧?(0点15分左右)

这篇文章可能会有所帮助。

不幸的是,我认为你不能用目前的 iOS SDK 工具来做到这一点,除非从头开始将 UITableView + 控制器组合在一起(你需要创建每一行本身,并有一个与行到的 CGRect 相关的 UITouch 响应) -移动)。

这会非常复杂,因为当您移动要重新排序的行时,您需要获得行的动画“让开”。

不过,cocoas 工具看起来很有前途,至少去看看源代码。