iOS - 如何查看表格视图单元格上的“滑动删除”操作?

fin*_*elp 4 uitableview swipe ios

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

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

环境:iOS 11+ 和 iPhone 应用程序。

这是一张图像,显示单元格滑动到一半,带有一个基本的“滑动删除”红色操作按钮。 通过部分滑动显示表格单元格下的删除操作

一位开发人员友好地提到了 SwipeCellKit,但 SwipeCellKit 有很多。我们想要做的只是简单地模拟部分滑动,让用户知道“滑动删除”的存在。换句话说,我们希望在单元格下的删除操作中提供一个潜行高峰。

如果有帮助,这里是SwipeCellKitshowSwipe代码链接这里是一个包含其使用示例的链接

我查看了 SwipeCellKit 源代码。我不清楚如何在没有 SwipeCellKit 的情况下做到这一点。此外,目前还不能选择使用 SwipeCellKit。

谷歌搜索没有帮助。我一直在研究如何添加滑动操作,但没有研究如何向用户简要显示单元格下的滑动操作,即 UITableViewRowAction 项目。

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

Ada*_*dam 7

我已经为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()
    }
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    tableView.presentSwipeHint()
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


jrt*_*ton 0

我很确定这是不可能的。滑动操作包含在 a 中UISwipeActionPullView,其中包含UISwipeStandardAction子视图,这两个视图都是私有的。它们也是表视图的一部分,而不是单元格的一部分,除非发生手势,否则不会添加它们,因此您不能将单元格撞到一侧并在那里看到它们。

在 UI 自动化测试之外,如果不使用私有 API,就不可能模拟用户手势,因此您无法“伪造”滑动,然后向用户显示结果。

然而,当你可以作弊时,为什么还要费力去“正确”地做呢?将单元格的内容视图稍微向左弹起并在红色框中弹起(不要太远以至于您可以看到文本,以避免本地化问题),然后恢复正常,应该不会太难。在第一次加载表视图时执行此操作,并在 N 次后或在用户证明他们知道此特定 iOS 约定如何工作后停止执行此操作。