Step 1 : Add your custom view in cell and set its frame as outside the cell
deleteView.frame = CGRect(x: cell.width, y: 0, width: 50, height: 50) // Here you need to set delete view outside the cell frame
Step 2 : You need to add tap gesture to you cell. Here I'm using https://github.com/SwipeCellKit/SwipeCellKit 's delegate method for handling swipe animations.
//Here You can use your custom logic for cell gesture animation
var swipeIndex: Int = 0 // Here this is global variable
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]?
{
if orientation == .right
{
if self.swipeIndex != indexPath.row
{
self.hideSwipeView(at: self.swipeIndex)
}
self.showSwipeView(at: indexPath.row)
}
else if orientation == .left
{
hideSwipeView(at: indexPath.row)
}
return nil
}
func hideSwipeView(at index:Int)
{
if (index < yourArray.count)
{
let cell = tableView.cellForRow(at: IndexPath.init(row: index, section: 0)) as! DetailTVCell
UIView.animate(withDuration: 0.1,
delay: 0,
options: [.curveEaseOut],
animations:
{
cell.viewCell.transform = CGAffineTransform(translationX: 0, y: 0) // Here viewCell is Main View inside Cell and all cell's content is inside viewCell
cell.deleteView.transform = CGAffineTransform(translationX: 0, y: 0)
}) { _ in
cell.btnDelete.isHidden = true
}
}
}
func showSwipeView(at index:Int)
{
let cell = tableView.cellForRow(at: IndexPath.init(row: index, section: 0)) as! DetailTVCell
cell.btnDelete.isHidden = false
UIView.animate(withDuration: 0.1,
delay: 0,
options: [.curveEaseOut],
animations:
{
cell.viewCell.transform = CGAffineTransform(translationX: -100, y: 0)
cell.deleteView.transform = CGAffineTransform(translationX: -100, y: 0)
}) { _ in
self.swipeIndex = index
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…