我想在按钮点击后将渐变颜色设置为背景以下是我的代码,我在上遇到崩溃,因为case将objGrediantView作为CAGradientLayer()在sender.subviews中
检查是否有Grediant
func setGrediantView(sender: AnyObject, topColor: UIColor, bottomColor: UIColor){
for case let objGrediantView as CAGradientLayer() in sender.subviews {
if objGrediantView {
// show error
return
}
}
let colorTop = topColor.cgColor
let colorBottom = bottomColor.cgColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [ colorTop, colorBottom]
gradientLayer.locations = [ 0.0, 1.0]
gradientLayer.frame = sender.bounds
sender.layer.insertSublayer(gradientLayer, at: 0)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.item)!")
let dicData = items[indexPath.item]
// self.objProfileView.backgroundColor = UIColor.white
self.setGrediantView(sender: objProfileView, topColor: (dicData["top"]!), bottomColor: (dicData["bottom"]!))
}
答案 0 :(得分:1)
重写文本(你的代码是不可编译的,我真的很想知道你是怎么做到的):
for sublayer in (sender.layer.sublayers ?? []) {
if sublayer is CAGradientLayer {
// show error
return
}
}