iOS - 如何查看表格 View 单元格上的 "Swipe To Delete"操作?
<p><p>当某个 Table View Controller 第一次显示时,如何简单的显示一个表格行中存在红色的“滑动删除”功能? </p>
<p>以编程方式玩 peekaboo 的目的是向用户展示该功能的存在。 </p>
<p>环境:iOS 11+ 和 iPhone 应用。</p>
<p>这是一张图片,显示单元格中途滑动,带有一个基本的“滑动删除”红色操作按钮。
<a href="/image/PSdER.jpg" rel="noreferrer noopener nofollow"><img src="/image/PSdER.jpg" alt="Showing delete action under table cell via partial swipe"/></a> </p>
<p>一位开发人员友好地提到了 SwipeCellKit,但 SwipeCellKit 有很多东西。我们要做的只是简单地模拟部分滑动,让用户知道“滑动删除”的存在。换句话说,我们希望在单元格下的删除操作中提供一个偷偷摸摸的峰值。</p>
<p>如果有帮助,这里是 <a href="https://github.com/SwipeCellKit/SwipeCellKit/blob/b724892f8c4daab931de1b5555de3373107db184/Source/SwipeTableViewCell%2BDisplay.swift#L37" rel="noreferrer noopener nofollow">SwipeCellKit's <code>showSwipe</code> code</a> 的链接这是 <a href="https://github.com/SwipeCellKit/SwipeCellKit/issues/95#issuecomment-334806269" rel="noreferrer noopener nofollow">link with an example of its use</a> .</p>
<p>我查看了 SwipeCellKit 源代码。如果没有 SwipeCellKit,我不清楚如何做到这一点。此外,目前还不能选择使用 SwipeCellKit。</p>
<p>谷歌搜索没有帮助。我不断遇到<a href="https://useyourloaf.com/blog/table-swipe-actions/" rel="noreferrer noopener nofollow">how to add swipe actions</a> ,但不是如何向用户简要地显示单元格下的滑动操作(即 UITableViewRowAction)项。</p>
<p>如何向用户简要展示这个内置的“滑动删除”操作?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我已经为 <code>UITableView</code> 编写了这个简单的扩展。它搜索可见单元格,找到包含编辑操作的第一行,然后“滑动”该单元格以显示下面的操作。您可以调整提示的宽度和持续时间等参数。</p>
<p>效果很好,因为它不会硬编码任何值,因此例如提示的背景颜色将始终与实际操作按钮匹配。</p>
<pre><code>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()
}
}
</code></pre>
<p>示例用法:</p>
<pre><code>override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableView.presentSwipeHint()
}
</code></pre>
<p> <a href="/image/oqkEJ.gif" rel="noreferrer noopener nofollow"><img src="/image/oqkEJ.gif" alt="enter image description here"/></a> </p></p>
<p style="font-size: 20px;">关于iOS - 如何查看表格 View 单元格上的"Swipe To Delete"操作?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/55592124/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/55592124/
</a>
</p>
页:
[1]