UITableView 在动画时剪辑到边界

San*_*áez 6 ios swift

我有一个带有阴影的单元格的 UITableView。当我做出平移手势以查看任何单元格的行操作时,表格视图会剪辑单元格的边界,直到手势完成。

我已经clipsToBounds设置为false我的单元格contentView和它的父,也为表视图。

我的自定义单元格代码:

var isExpanded: Bool = false

@IBOutlet weak var shadowView: UIView!
@IBOutlet weak var fullContainer: UIView!

override func awakeFromNib() {
    super.awakeFromNib()
    reloadUI()
}

func reloadUI() {
    contentView.superview?.clipsToBounds = false
    contentView.clipsToBounds = false

    fullContainer.layer.masksToBounds = true
    fullContainer.layer.cornerRadius = 8

    shadowView.layer.masksToBounds = false
    shadowView.layer.cornerRadius = 8
    shadowView.layer.shadowOpacity = 1.0
    shadowView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    shadowView.layer.shadowPath = UIBezierPath(roundedRect: fullContainer.bounds, cornerRadius: 8).cgPath
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    isExpanded = selected
    reloadUI()
}
Run Code Online (Sandbox Code Playgroud)

带有 tableview 的视图控制器的代码:

extension SecondViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let action = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
            tableView.deleteRows(at: [indexPath], with: .automatic)
        }
        return [action]
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.reloadRows(at: [indexPath], with: .automatic)
        selectedIndex = indexPath.section
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section != 0 ? 8 : 1
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.clipsToBounds = false
        return view
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if selectedIndex == indexPath.section {
            return 120
        }
        return 108
    }
}
Run Code Online (Sandbox Code Playgroud)

SecondViewController 也有这个代码,因为它有一个平移识别器,用于在带有手势的视图控制器之间进行更改

extension SecondViewController: UIGestureRecognizerDelegate {

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == recognizer || otherGestureRecognizer == recognizer {
            return false
        }
        return true
    }

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == recognizer{
            for cell in tableView.visibleCells {
                let location = gestureRecognizer.location(in: cell.contentView)
                if cell.contentView.layer.contains(location) {
                    return false
                }
            }
            return true
        }
        return true
    }
}
Run Code Online (Sandbox Code Playgroud)

UITableViewCell 的边界被剪掉了:

TableViewCell 的边界被剪掉了

Tam*_*gel 7

这看起来像是 UITableView 中的一个错误。它可以通过以下扩展解决:

extension UITableView {
    func fixCellBounds() {
        DispatchQueue.main.async { [weak self] in
            for cell in self?.visibleCells ?? [] {
                cell.layer.masksToBounds = false
                cell.contentView.layer.masksToBounds = false
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

只是把它打完电话后reloadRows/ insertRows/deleteRows等:

tableView.reloadRows(at: <index paths>, with: .automatic)
tableView.fixCellBounds()
Run Code Online (Sandbox Code Playgroud)

前:

修复前的单元格动画

后:

修复后的单元格动画