Swipe to Delete on a Tableview That Is Inside a Pageviewcontroller

UIPageViewController with UITableView, swipe to remove cells?

You won't be able to make the left swipe do two things.

The best solution would be to create an edit button that makes delete buttons appear next to your cells, like this:

Sample Image

The edit transforms into "Done" when tapped to remove the "delete mode".

An alternate way to do this would be to create a specific zone on your screen where the left swipe brings another VC, so you can use the rest of your screen to use left swipe on your elements to delete them.

Swipe to delete entire section in UITableView (iOS)

I haven't tested this but the idea is below. Take a view (self.header) and use the touchesBegan... method to detect the user placing their finger on screen. Then, follow the finger with the touchesMoved... method and calculate the difference between the last offset and the next. It should grow by 1 (or more) depending on how fast the user is moving their finger. Use this value to subtract the origin.x of the cell's contentView.

var header: UIView!
var tableView:UITableView!
var offset:CGFloat = 0

override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Touches Began. Disable user activity on UITableView
if let touch = touches.first {
// Get the point where the touch started
let point = touch.location(in: self.header)
offset = point.x
}
}

override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {

// Get the point where the touch is moving in header
let point = touch.location(in: self.header)

// Calculate the movement of finger
let x:CGFloat = offset - point.x
if x > 0 {
// Move cells by offset
moveCellsBy(x: x)
}

// Set new offset
offset = point.x
}
}

override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// Reset offset when user lifts finter
offset = 0
}

func moveCellsBy(x: CGFloat) {
// Move each visible cell with the offset
for cell in self.tableView.visibleCells {
// Place in animation block for smoothness
UIView.animate(withDuration: 0.05, animations: {
cell.contentView.frame = CGRect(x: cell.contentView.frame.origin.x - x, y: cell.contentView.frame.origin.y, width: cell.contentView.frame.size.width, height: cell.contentView.frame.size.height)
})
}
}

Access UIPageViewController gesture recognizer(s) to enable swipe to delete

The gestures are attached to its scrollView, and this one is not a public attribute. Anyway I use this extension to get the scrollView :

extension UIPageViewController {

public var scrollView: UIScrollView? {
for view in self.view.subviews {
if let scrollView = view as? UIScrollView {
return scrollView
}
}
return nil
}

}

Then you want its panGesture :

pageController.scrollView?.panGestureRecognizer


Related Topics



Leave a reply



Submit