首页 > 解决方案 > 滑动单元格时的 UITableViewCell 背景颜色动画

问题描述

我在滑动表格视图单元格时实现自定义动画时遇到问题。目标是在使用动画滑动单元格时更改单元格背景颜色。理想情况下,如果背景颜色根据 x 轴上的偏移量发生变化,那就完美了。或者如果不可能或很难做到,最好在滑动完成时完成动画。并且单元格的背景颜色应该与操作按钮的背景颜色相同。“滑动”意味着使按钮显示为默认删除操作的手势。

以下代码是当前代码。

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

        let action =  UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
            
            completionHandler(true)

        })

        action.image = UIGraphicsImageRenderer(size: CGSize(width: 30, height: 30)).image { _ in
            UIImage(named:"sell_btn")?.draw(in: CGRect(x: 0, y: 0, width: 30, height: 30))
        }
        action.backgroundColor = UIColor(hex: "#F4F4F4")
        tableView.backgroundColor = .red
        let confrigation = UISwipeActionsConfiguration(actions: [action])
        return confrigation
    }

此代码在调用此函数时更改背景颜色。

PS:我也尝试过 MGSwipeCell 和 SwipeCellKit,但它们并没有提供我想要的东西。

谢谢。

标签: iosswiftuitableviewuiviewanimationuitableviewrowaction

解决方案


推荐阅读