我正在尝试将一个项目从一个 UITableView 复制到另一个 View,过去 2 天我一直在努力解决这个问题,但我仍然无法弄清楚如何完成此操作。
这是我的 UI 架构的草图
这是我在做的事情
长按表格 View 中的一行
长按时创建单元格中图像的快照
将快照拖到表格 View 外的View(绿色区域)
发布时检查快照是否被放置在绿色 View 区。
我能够做到直到创建快照为止,当我尝试拖动快照时,我无法获得拖动快照的点。释放拖动时,我需要检查拖动的最后一点是否在 DROP AREA(绿色)内。
任何人都可以提供一些关于如何使用示例代码解决问题的见解......
提前致谢...
最佳答案
目前我没有时间测试代码,但它应该足够有意义......你可以这样做:
class ViewController: UIViewController, UITableViewDataSource {
private var dragView: UIView?
@IBOutlet weak var dropZone: UIView!
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell))
cell.contentView.addGestureRecognizer(lpGestureRecognizer)
return cell
}
func didLongPressCell (recognizer: UILongPressGestureRecognizer) {
switch recognizer.state {
case .Began:
if let cellView: UIView = recognizer.view {
cellView.frame.origin = CGPointZero
dragView = cellView
view.addSubview(dragView!)
}
case .Changed:
dragView?.center = recognizer.locationInView(view)
case .Ended:
if (dragView == nil) {return}
if (CGRectIntersectsRect(dragView!.frame, dropZone.frame)) {
if let cellView: UIView = (dragView?.subviews[0])! as UIView {
cellView.frame.origin = CGPointZero
dropZone.addSubview(cellView)
}
dragView?.removeFromSuperview()
dragView = nil
//Delete row from UITableView if needed...
} else {
//DragView was not dropped in dropszone... Rewind animation...
}
default:
print("Any other action?")
}
}
}
评论更新:
当然,一种可能是标记字段...像这样:
private let imageViewTag: Int = 997
private let textLabelTag: Int = 998
private let detailTtextLabelTag: Int = 999
//...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//...
cell.imageView?.tag = imageViewTag
cell.textLabel?.tag = textLabelTag
cell.detailTextLabel?.tag = detailTtextLabelTag
//...
}
func didLongPressCell (recognizer: UILongPressGestureRecognizer) {
//...
case .Ended:
let cellImageView: UIImageView? = recognizer.view?.viewWithTag(imageViewTag) as? UIImageView
let cellTextLabel: UITextField? = recognizer.view?.viewWithTag(textLabelTag) as? UITextField
let cellDetailTextLabel: UITextField? = recognizer.view?.viewWithTag(detailTtextLabelTag) as? UITextField
//...
}
关于ios - 从 TableView 拖放到 Swift 中的另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323948/