Swift – 使用Long Gesture识别器拖放TableViewCell

所以我已经看到很多关于重新sorting与“编辑模式”有关的单元格的post,但是没有解决我所遇到的问题。 (对不起,如果我错了)。

我正在构build一个排名应用程序,并寻找一种方法来使用长手势识别器来重新排列我的UITableView中的单元格。 基本上,用户将能够重新sorting和“sorting”与他们的朋友在一个组中的string充满string。

我会去在导航栏中使用“编辑”栏button项的标准路线,但我正在使用导航栏的右上angular添加新的string到tableview已经。 (下图描述了我的意思)。

到目前为止,我已经添加了`

var lpgr = UILongPressGestureRecognizer(target: self, action: "longPressDetected:") lpgr.minimumPressDuration = 1.0; tableView.addGestureRecognizer(lpgr)` 

我的viewDidLoad方法,并开始创build以下function:

  func longPressDetected(sender: AnyObject) { var longPress:UILongPressGestureRecognizer = sender as UILongPressGestureRecognizer var state:UIGestureRecognizerState = longPress.state let location:CGPoint = longPress.locationInView(self.tableView) as CGPoint var indexPath = self.tableView.indexPathForRowAtPoint(location)? var snapshot:UIView! var sourceIndexPath:NSIndexPath! } 

我在互联网上search到的所有资源最终都向我展示了一个巨大的,长期的添加剂列表,以获得所需的结果,但这些例子涉及核心数据。 在我看来,必须有一个更简单的方法来简单地重新排列tableview单元格与长按?

在这里输入图像说明

给这个教程一个镜头,你可能会在20分钟内运行:

伟大的Swift拖放教程

这很容易。 我只开发了3个月,我能够实现这一点。 我也尝试了其他几个,这是我能理解的。

它是用Swift编写的,实际上是剪切和粘贴的。 将longPress代码添加到viewDidLoad中,然后将该函数粘贴到类的“主体”中。 本教程将引导您,但没有更多的东西。

代码的快速解释 :此方法使用switch语句检测longPress是刚刚开始,已更改还是处于默认状态。 针对每种情况运行不同的代码。 它需要一个长按的单元格的快照/图片,隐藏你的单元格,并移动快照。 完成后,它将取消隐藏您的单元格,并从视图中删除快照。

警告 :我的一个小心的是,尽pipe这个拖放看起来很棒,而且完美地工作,但是在拖拽最低/最低单元格之下的单元格的时候,似乎出现了一个崩溃的问题。

拖放崩溃问题

戴夫的回答很好。 这里是本教程的快速4版本:

WayPointCell是你的CustomUITableViewCellwayPoints是UITableView的dataSource数组

首先把这个放在你的viewDidLoad里,就像Alfi提到的那样:

 override func viewDidLoad() { super.viewDidLoad() let longpress = UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureRecognized(gestureRecognizer:))) self.tableView.addGestureRecognizer(longpress) } 

然后执行该方法:

 func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) { let longpress = gestureRecognizer as! UILongPressGestureRecognizer let state = longpress.state let locationInView = longpress.location(in: self.tableView) var indexPath = self.tableView.indexPathForRow(at: locationInView) switch state { case .began: if indexPath != nil { Path.initialIndexPath = indexPath let cell = self.tableView.cellForRow(at: indexPath!) as! WayPointCell My.cellSnapShot = snapshopOfCell(inputView: cell) var center = cell.center My.cellSnapShot?.center = center My.cellSnapShot?.alpha = 0.0 self.tableView.addSubview(My.cellSnapShot!) UIView.animate(withDuration: 0.25, animations: { center.y = locationInView.y My.cellSnapShot?.center = center My.cellSnapShot?.transform = CGAffineTransform(scaleX: 1.05, y: 1.05) My.cellSnapShot?.alpha = 0.98 cell.alpha = 0.0 }, completion: { (finished) -> Void in if finished { cell.isHidden = true } }) } case .changed: var center = My.cellSnapShot?.center center?.y = locationInView.y My.cellSnapShot?.center = center! if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) { self.wayPoints.swapAt((indexPath?.row)!, (Path.initialIndexPath?.row)!) //swap(&self.wayPoints[(indexPath?.row)!], &self.wayPoints[(Path.initialIndexPath?.row)!]) self.tableView.moveRow(at: Path.initialIndexPath!, to: indexPath!) Path.initialIndexPath = indexPath } default: let cell = self.tableView.cellForRow(at: Path.initialIndexPath!) as! WayPointCell cell.isHidden = false cell.alpha = 0.0 UIView.animate(withDuration: 0.25, animations: { My.cellSnapShot?.center = cell.center My.cellSnapShot?.transform = .identity My.cellSnapShot?.alpha = 0.0 cell.alpha = 1.0 }, completion: { (finished) -> Void in if finished { Path.initialIndexPath = nil My.cellSnapShot?.removeFromSuperview() My.cellSnapShot = nil } }) } } func snapshopOfCell(inputView: UIView) -> UIView { UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0) inputView.layer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() let cellSnapshot : UIView = 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 } struct My { static var cellSnapShot: UIView? = nil } struct Path { static var initialIndexPath: IndexPath? = nil }