圆角视图上的阴影阴影不均匀

时间:2018-12-30 03:37:47

标签: ios swift uiview uicollectionview calayer

我有collectionview单元格,在该单元格中有一个容器UIView(),该容器在距超级视图一定距离的位置上留有边距。 containerview是四舍五入的,我正在尝试对其应用阴影。我很好地做到了这一点,直到在某些单元格上它打破了阴影层并创建了不均匀的阴影效果。以下是当前结果: enter image description here

以下是我的代码:

extension UIView() {
    func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
    self.layer.masksToBounds = false
    self.layer.shadowColor = color.cgColor
    self.layer.shadowOpacity = opacity
    self.layer.shadowOffset = offSet
    self.layer.shadowRadius = radius

    self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: radius).cgPath
    self.layer.shouldRasterize = true
    self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }
}

以下是UICollectionViewCell.swift

override func layoutSubviews() {
    super.layoutSubviews()
    containerView.dropShadow(color: .lightGray, opacity: 6, offSet: CGSize(width: 0, height: 0), radius: 6, scale: true)
}

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我在collectionview单元内的视图中使用了阴影。请参见下面的链接

View Sample

这是在cellforItem内

myCell.imgIcon.backgroundColor = UIColor.white
myCell.imgIcon.layer.shadowColor = UIColor.lightGray.cgColor
myCell.imgIcon.layer.shadowOpacity = 0.5
myCell.imgIcon.layer.shadowOffset = CGSize(width: 0, height: 0)
myCell.imgIcon.layer.shadowRadius = 5