在 UITableViewCell 上滑动时如何配置阈值/距离 [英] How to configure threshold/distance when swiping on UITableViewCell

查看:24
本文介绍了在 UITableViewCell 上滑动时如何配置阈值/距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have swipeable tableview cells. The intention is to let the user swipe rows left or right completely (swipe out completely), and the swiped row gets removed from the table (like how Inbox by Gmail works). Everything works fine, but I had a question.

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% 的方式)即可将行滑出?

How can I set the threshold/distance (how much the user has to swipe before the action is taken)? Currently, the user has to swipe half way, before the row gets swiped out. Can I change this point so the user only needs to swipe a little (say 20% of the way) to swipe out the row?

推荐答案

没有直接的配置方法.

解决方案

但您可以自己构建:

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

代码

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

A swipe of the cells to the left by 20%, leading to the deletion of a row, would look like this in the code:

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

演示

这篇关于在 UITableViewCell 上滑动时如何配置阈值/距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆