gpt4 book ai didi

ios - Swift - 使用长手势识别器拖放 TableViewCell

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:42 24 4
gpt4 key购买 nike

所以我看到了很多关于重新排序单元格的帖子,这些帖子与使用“编辑模式”有关,但没有一个是针对我遇到的问题的。 (如果我错了,请原谅)。

我正在构建一个排名应用程序,并寻找一种方法来使用长手势识别器对我的 UITableView 中的单元格重新排序。本质上,用户将能够与他们的 friend 一起对充满字符串的单元格进行重新排序和“排序”。

我会采用在导航栏中使用“编辑”栏按钮项的标准路线,但我已经在使用导航栏的右上角向表格 View 添加新字符串。 (下图描述了我的意思)。

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

    var lpgr = UILongPressGestureRecognizer(target: self, action: "longPressDetected:")

lpgr.minimumPressDuration = 1.0;
tableView.addGestureRecognizer(lpgr)`

到我的 viewDidLoad 方法,并开始创建以下函数:

    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!

}

我在 Internet 上搜索的所有资源最终都向我展示了一个巨大的、长长的用于该功能的添加剂列表,以便获得所需的结果,但这些示例涉及核心数据。在我看来,必须有一种更简单的方法来通过长按简单地重新排序 tableview 单元格?

enter image description here

最佳答案

Dave 的回答很棒。这是本教程的 swift 4 版本:

WayPointCell 是您的 CustomUITableViewCellwayPoints 是 UITableView 的数据源数组

首先,把它放在你的 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
}

关于ios - Swift - 使用长手势识别器拖放 TableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27631128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com