我是 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/