Ios 如何在swift中检查和设置grediant视图?
我想将渐变色设置为按钮上的背景色单击下面是我的代码,我在sender.subviews中将case let objGrediantView设置为CAGradientLayer() 检查是否有格雷迪安Ios 如何在swift中检查和设置grediant视图?,ios,iphone,swift,cagradientlayer,Ios,Iphone,Swift,Cagradientlayer,我想将渐变色设置为按钮上的背景色单击下面是我的代码,我在sender.subviews中将case let objGrediantView设置为CAGradientLayer() 检查是否有格雷迪安 func setGrediantView(sender: AnyObject, topColor: UIColor, bottomColor: UIColor){ for case let objGrediantView as CAGradientLayer() in sender.sub
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"]!))
}
重写文本如下(您的代码无法编译,我真的很想知道您是如何做到的):
你怎么回事啊?此外,为什么你说点击按钮,然后你调用它在
didSelectItemAt
?实际上这是在collectionView的按钮动作,这就是为什么,对不起我的英语。你的崩溃是什么?当我试图检查grediant层是否存在时,我得到了crashYes,谢谢它解决了我的问题:)
for sublayer in (sender.layer.sublayers ?? []) {
if sublayer is CAGradientLayer {
// show error
return
}
}