我想在具有4个部分(一个具有4个tableViewCell的tableView)的tableViewCell中添加阴影,其中最后一个单元格我想在3个侧面(左、右、下)添加阴影,但在除了最后一个单元格之外的其他单元格中,我只想在2个侧面(左、右)添加阴影。我在单例和UIView扩展中编写了此代码。
对于最后一个单元格:
func dropShadowAtBottom() {
self.layer.masksToBounds = false
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOpacity = 0.2
self.layer.opacity = 0.3
self.layer.shadowOffset = CGSize(width: 0, height: 2)
self.layer.shadowRadius = 2
self.clipsToBounds = false
}
除了最后一个单元格外,对于另一个单元格:
func dropShadowAtLeftAndRight() {
self.layer.masksToBounds = false
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOpacity = 0.5
self.layer.shadowOffset = CGSize(width: 0, height: 0)
self.layer.shadowRadius = 2
let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 4); // inset top/bottom
self.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath
self.layer.rasterizationScale = UIScreen.main.scale
self.layer.shouldRasterize = true
}
我在 cellForRow 方法中调用了这段代码:
if(indexPath.row == currentOrderHistory.listOrderItems.count - 1){
cell.bodyView.dropShadowAtBottomOnly()
}else{
cell.bodyView.dropShadowAtLeftAndRight()
}
return cell
目前这段代码可以工作但仍不完美。单元格之间有空白。
我只想让它们连接在一起。