ios - 在 UITableViewCell 中绘制阴影会导致视觉错误

标签 ios swift uitableview

我是 swift 的初学者。我想在 UITableViewCell 中的 View 后面画一个阴影,但是当我添加以下代码时:

func doTheWork(statutOfCard : statut){
    switch statutOfCard {
    case .selectPicture:
        cardViewHeightCon.constant = 300
        if ShadowLayerCard == nil{
            self.addCardShadow()
        }
    case .modifyPicture:
        cardViewHeightCon.constant = 400
        if ShadowLayerCard == nil{
            self.addCardShadow()
        }
    default:
        cardViewHeightCon.constant = 300
        if ShadowLayerCard == nil{
            self.addCardShadow()
        }
    }
}

func addCardShadow(){
    let shadowLayer = CAShapeLayer()
    ShadowLayerCard = shadowLayer
    shadowLayer.path = UIBezierPath(roundedRect: CGRect(0, 0, widthOfDevice! - (widthMarginConstraint*2), cardViewHeightCon.constant), cornerRadius: 10).cgPath
    shadowLayer.fillColor = UIColor(rgb: 0x00ff33).cgColor
    shadowLayer.shadowPath = shadowLayer.path
    shadowLayer.shadowColor = UIColor.black.cgColor
    shadowLayer.shadowRadius = 5
    shadowLayer.shadowOpacity = 0.2
    shadowLayer.shadowOffset = CGSize(width: 0, height: 0)
    cardView.layer.insertSublayer(shadowLayer, at: 0)
}

阴影出现在 UITableViewCell 中,但是当我向下滚动时,会产生这样的视觉错误:

我认为阴影是在 cardView 的高度更新之前绘制的,但我该如何解决这个问题?如果我需要以编程方式添加另一个组件(如图片),我可以在 doTheWork 函数中编写吗?或者这会再次产生错误吗?

最佳答案

将阴影添加到 cardView.layer 并在创建单元格时执行一次。您可能需要另一个 View 来保存带有圆角的图像,因为您需要将 clip 设置为 bound true for corners。

func addCardShadow(){
    let shadowLayer = cardView.layer
    shadowLayer.shadowColor = UIColor.black.cgColor
    shadowLayer.shadowRadius = 5
    shadowLayer.shadowOpacity = 0.2
    shadowLayer.shadowOffset = CGSize(width: 2, height: 2)
}

关于ios - 在 UITableViewCell 中绘制阴影会导致视觉错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51512585/

相关文章:

ios - 在 UIViewController 中使用自定义 UITableView

iOS开发-无限层级的uitableview

ios - 如何以 3D 方式旋转 UIView?

ios - 自定义导航栏按钮和打开新 viewController 的操作

ios - 从 UITextField 输入验证 NSDate

ios - 将 subview 添加到 ScrollView 时遇到问题

ios - 当我从另一个 View 返回 View 时 TabBarController 消失

xcode - 当一个 .Swift 文件的图标比另一个文件的图标暗时,这意味着什么?

swift - 执行身份验证后如何重试请求?

ios - 键盘显示额外空间 Swift