【问题标题】:How to configure threshold/distance when swiping on UITableViewCell在 UITableViewCell 上滑动时如何配置阈值/距离
【发布时间】:2019-04-15 08:29:44
【问题描述】:

我有可滑动的表格视图单元格。目的是让用户完全向左或向右滑动行(完全向外滑动),然后从表格中删除滑动的行(就像 Gmail 收件箱的工作方式一样)。一切正常,但我有一个问题。

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let swipeRightAction = UIContextualAction(style: .destructive, title:  "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        let item = self.myItems[indexPath.row]
        self.swipeRight(item) //do something
        self.myItems.remove(at: indexPath.row)
        success(true)
    })

    return UISwipeActionsConfiguration(actions: [swipeRightAction])
 }

如何设置阈值/距离(用户必须滑动多少才能采取行动)?目前,用户必须在该行被刷出之前滑动一半。我可以更改这一点,让用户只需轻扫一点(比如 20% 的距离)即可扫出该行吗?

【问题讨论】:

  • 1.从来不知道有什么叫UISwipeActionsConfiguration :) 2. 你见过这样的应用程序吗? 3. 整体有趣的问题。赞成
  • 虽然这不能解决您的问题,但我敦促您考虑这一点。 20% 的滑动删除“可能”会导致在用户滚动您的 tableview 时注册不正确的删除,而不是中途默认滑动,这更清楚地表明用户的意图是删除单元格。
  • @Honey:它是在 iOS 11 的 UITableView 中引入的,所以它很新。有关此 API 的更多信息here
  • @Vin Gazoil:那篇文章与这个问题无关,以及其他大多数文章都只涉及每个人都知道的基本功能。

标签: ios swift uitableview


【解决方案1】:

没有直接的配置方法。

解决方案

但你可以自己构建它:

  • 将 UIPanGestureRecognizer 添加到表中
  • 允许同时识别手势
  • 当滑动结束时检查方向并计算平移的百分比
  • 确定受影响的表行
  • 删除它

代码

将单元格向左滑动 20%,导致删除一行,在代码中如下所示:

class ViewController: UITableViewController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        let swipeGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(onSwiped(_:)))
        swipeGestureRecognizer.delegate = self
        self.tableView.addGestureRecognizer(swipeGestureRecognizer)
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

    @objc private func onSwiped(_ gestureRecognizer: UIPanGestureRecognizer) {
        if gestureRecognizer.state == .ended {
            let translation = gestureRecognizer.translation(in: self.view)
            guard translation.x < 0 else { return }
            let width = self.tableView.bounds.width
            let percentage = -translation.x / width
            print("swiped left percentage:\(percentage)")
            if percentage > 0.2 {
                let location = gestureRecognizer.location(in: self.tableView)
                if let indexPath = self.tableView.indexPathForRow(at: location) {
                    print("delete row: \(indexPath.row)")
                    self.dataSource.remove(at: indexPath.row)
                    self.tableView.reloadData()
                }
            }
        }
    }

演示

【讨论】:

  • 这将与leadingSwipeActionsConfigurationForRowAt 结合使用,对吗?
  • 此示例将与 trailingSwipeActionsConfigurationForRowAt 结合使用,因为我们向左滑动,但向右滑动可以使用相同的机制(将保护更改为 translation.x > 0 和 percent =翻译.x / 宽度)
猜你喜欢
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
相关资源
最近更新 更多