Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置高度动画时,阴影/单元闪烁_Ios_Swift_Uitableview - Fatal编程技术网

Ios 设置高度动画时,阴影/单元闪烁

Ios 设置高度动画时,阴影/单元闪烁,ios,swift,uitableview,Ios,Swift,Uitableview,我的单元格上的阴影有问题(点击GIF查看动画): 当我调用beginUpdate时,阴影闪烁: cell.tappedParentView .subscribe(onNext: { [weak self] in guard let self = self else { return } cell.animate() self.tableView.beginUpdates() self.tableView.endUpdate

我的单元格上的阴影有问题(点击GIF查看动画):

当我调用
beginUpdate
时,阴影闪烁:

cell.tappedParentView
    .subscribe(onNext: { [weak self] in
        guard let self = self else { return }
        cell.animate()
        self.tableView.beginUpdates()
        self.tableView.endUpdates()

        self.expandedIndexPaths[indexPath] = !self.expandedIndexPaths[indexPath].or(false)
    })
    .disposed(by: cell.cellBag)
单元格中的动画代码<代码>动画如下所示:

func animate() {
    spacerViewHeightConstraint?.constant = isExpanded ? 0.0 : 8.0

    parentView.updateLayout(isExpanded: !self.isExpanded)
    UIView.animate(withDuration: 0.2) {
           self.childViews.forEach {
               $0.isHidden = self.isExpanded
               $0.alpha = self.isExpanded ? 0.0 : 1.0
           }

         self.layoutIfNeeded()
    }

    isExpanded.toggle()
}
updateLayout

func updateLayout(isExpanded: Bool, animated: Bool = true) {
    stackViewLeadingConstraint.constant = isExpanded ? 8.0 : 16.0
    stackViewTrailingConstraint.constant = isExpanded ? 8.0 : 16.0
    stackViewTopConstraint.constant = isExpanded ? 8.0 : 16.0
    stackViewBottomConstraint.constant = isExpanded ? 0.0 : 16.0

    let layout = {
        self.layoutIfNeeded()

        self.shadow = isExpanded ? nil : .card
        self.backgroundImageView.alpha = isExpanded ? 0.0 : 1.0
        self.containerView.backgroundColor = isExpanded ? .clear : .red
        self.titleLabel.textColor = isExpanded ? Theme.text100 : .white
        self.descLabel.textColor = isExpanded ? Theme.text100 : .white

        self.startingLabel.alpha = isExpanded ? 0.0 : 1.0
        self.startingLabel.isHidden = isExpanded

        self.priceLabel.alpha = isExpanded ? 0.0 : 1.0
        self.priceLabel.isHidden = isExpanded

        self.showLessButton.isHidden = !isExpanded
        self.showLessButton.alpha = isExpanded ? 1.0 : 0.0

        self.stackView.spacing = isExpanded ? 8.0 : 4.0
    }

    if animated {
        UIView.animate(withDuration: 0.2) { layout() }
    } else {
        layout()
    }
}
如果我执行动画时没有调用
self.tableView.beginUpdate()
self.tableView.endUpdates()
,则不会发生闪烁,但单元格高度当然不会相应调整。我真的不知道如何解决这个问题。所有视图和单元格都有透明背景(只有表格视图背景是纯色)<对于每个单元格/视图中的
视图
内容视图
,code>cliptobunds处于禁用状态。我也尝试过使用纯色作为背景,但这似乎没有任何区别。我怎样才能解决这个问题


任何指点都将不胜感激!谢谢

我的答案就是从这个问题中得到启发的

我花了两天时间才找到答案。最后,我注意到,当我调用
tableView.reloadRows(在:[indexPath],使用:.fade)
或仅仅调用
tableView.beginUpdates()
tableView.endUpdates()
时,如果使用XCode中的调试视图层次结构暂停视图,由于某些原因,单元格仅在重新加载过程中才有其
clipstobunds=true
,即使我将其显式设置为
false

对我有效的解决方案是:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.clipsToBounds = false
    cell.layer.masksToBounds = false
    cell.contentView.layer.masksToBounds = false
}

我假设动画正在对表格进行重新渲染。阴影扩散是一种很重的渲染。因此,您可以尝试将其光栅化:
shadowView.layer.shouldRasterize=true
或将带有阴影的图像移到表外。但这只是假设。尝试过应该光栅化并且没有改变