Я пытаюсь переместить ячейку tableView с помощью longPressGesture.
Он хорошо работает, пока местоположение отправителя находится в tableView.
И проблема в том, что когда я освобождаю перетаскивание из tableView, снимок фиксируется stat.default не работает.
Мне нужна ваша помощь. Спасибо!
![enter image description here](https://i.stack.imgur.com/VEM6E.png)
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
}})
}
}