-4

I want to drag the cell out of screen in left direction to delete it. I don't want to show the delete option of table view edit mode. Showing delete option is very old and traditional method thats why I want to do this with the help of gestures.

AXY
  • 46
  • 7
  • Refer this [link](https://stackoverflow.com/questions/3309484/uitableviewcell-show-delete-button-on-swipe) – Aditya Srivastava Aug 14 '17 at 10:36
  • 1
    Possible duplicate of [UITableViewCell, show delete button on swipe](https://stackoverflow.com/questions/3309484/uitableviewcell-show-delete-button-on-swipe) – Idan Aug 14 '17 at 10:37

1 Answers1

1

Add this to you program:

let recognizer = UIPanGestureRecognizer(target: self, action: Selector(("handlePan")))
recognizer.delegate = self
addGestureRecognizer(recognizer)

And then make method to use this recognizer:

func handlePan(recognizer: UIPanGestureRecognizer) {
     if recognizer.state == .began {
        originalCenter = center
     }
     if recognizer.state == .changed {
        let transition = recognizer.translation(in: self)
        center = CGPoint(x: originalCenter.x + transition.x, y: originalCenter.y)
        deleteOnDragRelease = frame.origin.x < -frame.size.width / 2.0
     }
     if recognizer.state == .ended {
        let originalFrame = CGRect(x: 0, y: frame.origin.y, width: bounds.size.width, height: bounds.size.height)
        if !deleteOnDragRelease {
            UIView.animate(withDuration: 0.2, animations: {self.frame = originalFrame})
        }
        if deleteOnDragRelease {
            if delegate != nil && toDoItem != nil {
               delegate!.toDoItemDeleted(todoitem: toDoItem!)
            }
        }
    }
}

i think you can get all your answers for this doubt on the link given below:

https://www.raywenderlich.com/77974/making-a-gesture-driven-to-do-list-app-like-clear-in-swift-part-1

Vivek Tyagi
  • 168
  • 1
  • 4
  • 15