В настоящее время у меня есть UITableView
с элементами из массива. Можно перемещать позицию UITableViewCell
с UILongPressGestureRecognizer
свободно вокруг оси Y и X, однако я хочу, чтобы иметь возможность удалять перетаскиваемый UITableViewCell
, если он перемещен за пределы UITableView
.(Swift) Удаление UITableViewCell путем перетаскивания его из UITableView?
Я в основном знаю, что я буду использовать случай коммутатора UIGestureRecognizer.Ended
, чтобы выполнить это, но я сейчас не в состоянии справиться с этим.
Вот код:
func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {
let longPress = gestureRecognizer as! UILongPressGestureRecognizer
let state = longPress.state
var locationInView = longPress.locationInView(tblTasks)
var indexPath = tblTasks.indexPathForRowAtPoint(locationInView)
struct My {
static var cellSnapshot: UIView? = nil
}
struct Path {
static var initialIndexPath : NSIndexPath? = nil
}
switch state {
case UIGestureRecognizerState.Began:
if indexPath != nil {
Path.initialIndexPath = indexPath
let cell = tblTasks.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
My.cellSnapshot = snapshotOfCell(cell)
var center = cell.center
My.cellSnapshot!.center = center
My.cellSnapshot!.alpha = 0.0
tblTasks.addSubview(My.cellSnapshot!)
UIView.animateWithDuration(0.25, animations: {() -> Void in
center.y = locationInView.y
My.cellSnapshot!.center = center
My.cellSnapshot!.transform = CGAffineTransformMakeScale(1.05, 1.05)
My.cellSnapshot!.alpha = 0.98
cell.alpha = 0.0
}, completion: { (finished) -> Void in
if finished {
cell.hidden = true
}
}) // End of animation sequence
} // End of IF statement
case UIGestureRecognizerState.Changed:
var center = My.cellSnapshot!.center
center.x = locationInView.x
center.y = locationInView.y
My.cellSnapshot!.center = center
if (indexPath != nil) && (indexPath != Path.initialIndexPath) {
swap(&taskMgr.tasks[indexPath!.row], &taskMgr.tasks[Path.initialIndexPath!.row])
tblTasks.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)
Path.initialIndexPath = indexPath
}
default:
let cell = tblTasks.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell!
cell.hidden = false
cell.alpha = 0.0
UIView.animateWithDuration(0.25, animations: {() -> Void in
My.cellSnapshot!.center = cell.center
My.cellSnapshot!.transform = CGAffineTransformIdentity
My.cellSnapshot!.alpha = 0.0
cell.alpha = 1.0
}, completion: { (finished) -> Void in
if finished {
Path.initialIndexPath = nil
My.cellSnapshot!.removeFromSuperview()
My.cellSnapshot = nil
}
})
} // End of switch
} // End of longPressGestureRecognized func
Любые идеи?
Вы имеете в виду, например, прокручивая ячейку влево и удаляя ее? – Eendje
Это не то, что я ищу. У меня есть GestureRecognizer, который позволяет мне переупорядочивать ячейки, удерживая ячейку и перемещая ее в виде таблицы. То, что я действительно хочу в стороне от этого, - это перетащить ячейку, которую я держу за пределами представления таблицы, и, когда она выходит за пределы представления таблицы, я хочу, чтобы она была удалена. – wibbed
Возможно, используйте Key Value Observing, чтобы наблюдать за положением ячейки представления таблицы, когда она проходит границы таблицы, удаляет ее и анимирует табличное представление? – GangstaGraham