在 UITableView (iOS) 中滑动以删除整个部分 [英] Swipe to delete entire section in UITableView (iOS)
问题描述
我过去使用 MGSwipeTableCell 滑动以关闭单元格取得了很多成功,但我当前的任务要求以相同的行为滑动整个部分.
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.
我目前在 UITableView 中有一个滑动手势识别器,当滑动手势被触发时,我计算收到触摸的部分,并删除填充该部分的对象(在核心数据中),然后调用删除动画:
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:
//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)
这可行,但并不理想.理想情况下,我们希望用手指拖动整个部分(并且在某个点松开时,它会离开屏幕),类似于 MGSwipeTableCell.解决这个问题的最佳方法是什么?是否有另一个库允许滑动删除部分(我找不到任何部分)?或者这是我必须自己创建的东西.
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. 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.
推荐答案
我还没有测试过这个,但想法如下.获取视图 (self.header
) 并使用 touchesBegan...
方法检测用户将手指放在屏幕上.然后,用 touchesMoved...
方法跟随手指并计算上一个偏移量和下一个偏移量之间的差异.它应该增长 1(或更多),具体取决于用户移动手指的速度.使用此值减去单元格的 contentView
的 origin.x
.
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)
})
}
}
这篇关于在 UITableView (iOS) 中滑动以删除整个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!