gpt4 book ai didi

iOS - 如何查看 TableView 单元格上的 "Swipe To Delete"操作?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:59:11 25 4
gpt4 key购买 nike

当某个Table View Controller 第一次显示时,如何简单地显示表格行中存在红色的“滑动删除”功能?

以编程方式玩躲猫猫的目的是向用户展示该功能的存在。

环境:iOS 11+ 和 iPhone 应用。

这是一张显示单元格滑动到一半的图像,带有基本的“滑动以删除”红色操作按钮。 Showing delete action under table cell via partial swipe

一位开发人员友善地提到了 SwipeCellKit,但 SwipeCellKit 的功能很多。我们想要做的只是简单地模拟部分滑动,让用户知道“滑动删除”的存在。换句话说,我们希望提供单元格下删除操作的先睹为快。

如果有帮助,这里是 SwipeCellKit's showSwipe code 的链接这是一个link with an example of its use .

我查看了 SwipeCellKit 源代码。我不清楚如何在没有 SwipeCellKit 的情况下进行操作。此外,目前无法使用 SwipeCellKit。

谷歌搜索没有帮助。我一直遇到 how to add swipe actions ,而不是如何向用户简要显示单元格下的滑动操作 aka UITableViewRowAction 项目。

如何向用户简要展示这个内置的“滑动删除”操作?

最佳答案

我为 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()
}

enter image description here

关于iOS - 如何查看 TableView 单元格上的 "Swipe To Delete"操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com