[英]Swipe to delete entire section in UITableView (iOS)
I've had a lot of success in the past using MGSwipeTableCell to swipe to dismiss cells, but my current task calls to swipe an entire section in the same behavior.我在过去使用MGSwipeTableCell滑动以关闭单元格取得了很多成功,但我当前的任务调用以相同的行为滑动整个部分。
I currently have a swipe gesture recognizer in the UITableView, when the swipe gesture is triggered, I calculate the section the touch was recieved, and delete the objects that populate that section (in core data), then call the delete animation:我目前在 UITableView 中有一个滑动手势识别器,当触发滑动手势时,我计算收到触摸的部分,并删除填充该部分的对象(在核心数据中),然后调用删除动画:
//Delete objects that populate table datasource
for notification in notifications {
notificationObject.deleted = true
}
DataBaseManager.sharedInstance.save()
let array = indexPathsToDelete
let indexSet = NSMutableIndexSet()
array.forEach(indexSet.add)
//Delete section with animation
self.notificationsTableView.deleteSections(indexSet as IndexSet, with: .left)
This works, but is not ideal.这有效,但并不理想。 Ideally we would like the whole section to drag with your finger (and when released at a certain point, it goes off screen), similar to MGSwipeTableCell.理想情况下,我们希望用手指拖动整个部分(并且在某个点松开时,它会离开屏幕),类似于 MGSwipeTableCell。 What is the best way to approach this?解决这个问题的最佳方法是什么? Is there another library which allows swipe to delete sections (I can't find any)?是否有另一个库允许滑动删除部分(我找不到任何部分)? Or is this something I will have to create myself.或者这是我必须自己创造的东西。
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.查看 ( self.header
) 并使用touchesBegan...
方法来检测用户将手指放在屏幕上。 Then, follow the finger with the touchesMoved...
method and calculate the difference between the last offset and the next.然后,用touchesMoved...
方法跟随手指并计算上一个偏移量和下一个偏移量之间的差异。 It should grow by 1 (or more) depending on how fast the user is moving their finger.它应该增长 1(或更多),具体取决于用户移动手指的速度。 Use this value to subtract the origin.x
of the cell's contentView
.使用此值减去单元格contentView
的origin.x
。
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)
})
}
}
Brandon's answer is correct, however, INSPullToRefresh library has issues when using touches began and other touch delegate methods. Brandon 的回答是正确的,但是,INSPullToRefresh 库在使用开始触摸和其他触摸委托方法时存在问题。
What I had to do was implement a UIPanGestureRecognizer and track the touch when that gesture recognizer event is fired我必须做的是实现一个 UIPanGestureRecognizer 并在触发手势识别器事件时跟踪触摸
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.