我有UICollectionView,中心单元格将突出显示,如下图所示。
我使用以下代码在中心单元格周围制作阴影。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : STSuggestStyleCell = collectionView.dequeueReusableCell(for: indexPath)
if indexPath.row == 4{
cell.imageProfile.layer.cornerRadius = 2.0
cell.imageProfile.layer.borderWidth = 1.0;
cell.imageProfile.layer.borderColor = UIColor.clear.cgColor
cell.imageProfile.layer.masksToBounds = true;
cell.imageProfile.layer.shadowColor = UIColor.lightGray.cgColor;
cell.imageProfile.layer.shadowOffset = CGSize(width: 2.0, height: 2.0);
cell.imageProfile.layer.shadowRadius = 2.0;
cell.imageProfile.layer.shadowOpacity = 2.0;
cell.imageProfile.layer.masksToBounds = false;
cell.imageProfile.layer.shadowPath = UIBezierPath(roundedRect: cell.imageProfile.bounds, cornerRadius: cell.imageProfile.layer.cornerRadius).cgPath
cell.imageProfile.backgroundColor = UIColor.white
}
return cell
}
但它无法正常工作。
有谁能告诉我如何使用UICollectionView实现上述设计?
答案 0 :(得分:0)
查看中心单元格的红线
细胞本身没有阴影效果。阴影在相邻的细胞上。所以你需要做的就是要求细胞覆盖"覆盖"在其他细胞上。
只需拨打cell.clipToBounds = true
即可让您的中心小区覆盖在相邻小区上。然后确保将阴影效果从中心单元格中消失。
答案 1 :(得分:0)
也许你需要创建两个不同的单元格。因为它的重用功能。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.row == 4{
let cell : STSuggestStyleCell = collectionView.dequeueReusableCell(for: indexPath)
cell.imageProfile.layer.cornerRadius = 2.0
cell.imageProfile.layer.borderWidth = 1.0;
cell.imageProfile.layer.borderColor = UIColor.clear.cgColor
cell.imageProfile.layer.masksToBounds = true;
cell.imageProfile.layer.shadowColor = UIColor.lightGray.cgColor;
cell.imageProfile.layer.shadowOffset = CGSize(width: 2.0, height: 2.0);
cell.imageProfile.layer.shadowRadius = 2.0;
cell.imageProfile.layer.shadowOpacity = 2.0;
cell.imageProfile.layer.masksToBounds = false;
cell.imageProfile.layer.shadowPath = UIBezierPath(roundedRect: cell.imageProfile.bounds, cornerRadius: cell.imageProfile.layer.cornerRadius).cgPath
cell.imageProfile.backgroundColor = UIColor.white
return cell
}else{
let cell : STSuggestNomalCell = collectionView.dequeueReusableCell(for: indexPath)
return cell
}
}