我正在尝试使用longPressGesture移动tableView单元格。
当发件人位置在tableView中时,它工作良好。
问题是当我释放拖出tableView的拖动时,快照被冻结。 stat.default不起作用。
我需要你的帮助。谢谢!
var originIndexPath: IndexPath?
var initialSnapshot: UIView?
@IBAction func LongPress(_ sender: UILongPressGestureRecognizer) {
let state = sender.state
let location = sender.location(in: tableView)
guard let indexPath = self.tableView.indexPathForRow(at: location) else { return }
switch state {
case .began:
originIndexPath = indexPath
guard let cell = self.tableView.cellForRow(at: indexPath) else { return }
initialSnapshot = cellSnapshot(inputView: cell)
guard let snapshot = initialSnapshot else { return }
var center = cell.center
snapshot.center = center
snapshot.alpha = 0.0
self.tableView.addSubview(snapshot)
UIView.animate(withDuration: 0.25, animations: {
center.y = location.y
snapshot.center = center
snapshot.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
snapshot.alpha = 0.98
cell.alpha = 0.0
}, completion:{(finished) in cell.isHidden = true })
case .changed:
guard let snapshot = initialSnapshot else { return }
var center = snapshot.center
center.y = location.y
snapshot.center = center
guard let sourceIndexPath = originIndexPath else { return }
if indexPath.section != sourceIndexPath.section {
if indexPath.row + 1 == tableView.numberOfRows(inSection: indexPath.section){
originIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)
}else{ originIndexPath = indexPath }
let cataitem = fetchedResultsController.object(at: sourceIndexPath )
cataitem.category = NSNumber(value: indexPath.section).boolValue
saveContext()
}
else if indexPath.row != sourceIndexPath.row {
tableView.moveRow(at: sourceIndexPath, to: indexPath)
originIndexPath = indexPath
}
default:
guard let cell = self.tableView.cellForRow(at: originIndexPath!) else {return}
guard let snapshot = initialSnapshot else {return}
cell.isHidden = false
cell.alpha = 0.0
UIView.animate(withDuration: 0.25, animations: {
snapshot.center = cell.center
snapshot.transform = CGAffineTransform.identity
snapshot.alpha = 0
cell.alpha = 1
}, completion: { (finished) -> Void in
if finished {
self.originIndexPath = nil
snapshot.removeFromSuperview()
self.initialSnapshot = nil
}})
}
}