• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:41:32 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

环境: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 ,但不是如何向用户简要地显示单元格下的滑动操作(即 UITableViewRowAction)项。

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



Best Answer-推荐答案


我已经为 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 - 如何查看表格 View 单元格上的 "Swipe To Delete"操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592124/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap