当某个 Table View Controller 第一次显示时,如何简单的显示一个表格行中存在红色的“滑动删除”功能?
以编程方式玩 peekaboo 的目的是向用户展示该功能的存在。
环境:iOS 11+ 和 iPhone 应用。
这是一张图片,显示单元格中途滑动,带有一个基本的“滑动删除”红色操作按钮。
一位开发人员友好地提到了 SwipeCellKit,但 SwipeCellKit 有很多东西。我们要做的只是简单地模拟部分滑动,让用户知道“滑动删除”的存在。换句话说,我们希望在单元格下的删除操作中提供一个偷偷摸摸的峰值。
如果有帮助,这里是 SwipeCellKit's showSwipe code 的链接这是 link with an example of its use .
我查看了 SwipeCellKit 源代码。如果没有 SwipeCellKit,我不清楚如何做到这一点。此外,目前还不能选择使用 SwipeCellKit。
谷歌搜索没有帮助。我不断遇到how to add swipe actions ,但不是如何向用户简要地显示单元格下的滑动操作(即 UITableViewRowAction)项。
如何向用户简要展示这个内置的“滑动删除”操作?
Best Answer-推荐答案 strong>
我已经为 UITableView 编写了这个简单的扩展。它搜索可见单元格,找到包含编辑操作的第一行,然后“滑动”该单元格以显示下面的操作。您可以调整提示的宽度和持续时间等参数。
效果很好,因为它不会硬编码任何值,因此例如提示的背景颜色将始终与实际操作按钮匹配。
import UIKit
extension UITableView {
/**
Shows a hint to the user indicating that cell can be swiped left.
- Parameters:
- width: Width of hint.
- duration: Duration of animation (in seconds)
*/
func presentTrailingSwipeHint(width: CGFloat = 20, duration: TimeInterval = 0.8) {
var actionPath: IndexPath?
var actionColor: UIColor?
guard let visibleIndexPaths = indexPathsForVisibleRows else {
return
}
if #available(iOS 13.0, *) {
// Use new API, UIContextualAction
for path in visibleIndexPaths {
if let config = delegate?.tableView?(self, trailingSwipeActionsConfigurationForRowAt: path), let action = config.actions.first {
actionPath = path
actionColor = action.backgroundColor
break
}
}
} else {
for path in visibleIndexPaths {
if let actions = delegate?.tableView?(self, editActionsForRowAt: path), let action = actions.first {
actionPath = path
actionColor = action.backgroundColor
break
}
}
}
guard let path = actionPath, let cell = cellForRow(at: path) else { return }
cell.presentTrailingSwipeHint(actionColor: actionColor ?? tintColor)
}
}
fileprivate extension UITableViewCell {
func presentTrailingSwipeHint(actionColor: UIColor, hintWidth: CGFloat = 20, hintDuration: TimeInterval = 0.8) {
// Create fake action view
let dummyView = UIView()
dummyView.backgroundColor = actionColor
dummyView.translatesAutoresizingMaskIntoConstraints = false
addSubview(dummyView)
// Set constraints
NSLayoutConstraint.activate([
dummyView.topAnchor.constraint(equalTo: topAnchor),
dummyView.leadingAnchor.constraint(equalTo: trailingAnchor),
dummyView.bottomAnchor.constraint(equalTo: bottomAnchor),
dummyView.widthAnchor.constraint(equalToConstant: hintWidth)
])
// This animator reverses back the transform.
let secondAnimator = UIViewPropertyAnimator(duration: hintDuration / 2, curve: .easeOut) {
self.transform = .identity
}
// Don't forget to remove the useless view.
secondAnimator.addCompletion { position in
dummyView.removeFromSuperview()
}
// We're moving the cell and since dummyView
// is pinned to cell's trailing anchor
// it will move as well.
let transform = CGAffineTransform(translationX: -hintWidth, y: 0)
let firstAnimator = UIViewPropertyAnimator(duration: hintDuration / 2, curve: .easeIn) {
self.transform = transform
}
firstAnimator.addCompletion { position in
secondAnimator.startAnimation()
}
// Do the magic.
firstAnimator.startAnimation()
}
}
示例用法:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableView.presentSwipeHint()
}
关于iOS - 如何查看表格 View 单元格上的 "Swipe To Delete"操作?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/55592124/
|