Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中检查和设置grediant视图?_Ios_Iphone_Swift_Cagradientlayer - Fatal编程技术网

Ios 如何在swift中检查和设置grediant视图?

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

我想将渐变色设置为按钮上的背景色单击下面是我的代码,我在sender.subviews中将case let objGrediantView设置为CAGradientLayer()

检查是否有格雷迪安

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
        }
    }