UIbutton系列1 --- 渐变色+失效属性

一、渐变色

 // 渐变色
    public func addGradient(fromColor : UIColor,
                            toColor : UIColor,
                            frame : CGRect,
                            fromPoint : CGPoint = CGPoint(x: 0.0, y: 0.0),
                            toPoint : CGPoint = CGPoint(x: 1, y: 0.5)) {
    
    
        let gradient = CAGradientLayer()
        gradient.colors = [fromColor.cgColor, toColor.cgColor]
        gradient.startPoint = fromPoint
        gradient.endPoint = toPoint
        gradient.frame = frame
        self.layer.insertSublayer(gradient, at: 0)
    }
//调用方法
Button.addGradient(fromColor: UIColor.init(normal: "#FFD767"), toColor: UIColor.init(normal: "#FABC11"), frame: CGRect.init(x: 0, y: 0, width: kScreenWidth - 72, height: 46), fromPoint: CGPoint.init(x: 0, y: 0.5), toPoint: CGPoint.init(x: 1, y: 0.5))

二、失效属性

        // 当按钮失效时图像是否降低亮度
        Button.adjustsImageWhenDisabled = true
        Button.layer.masksToBounds = true
        Button.setTitleColor(UIColor.red, for: .normal)
        Button.setTitleColor(UIColor.black, for: .disabled)
        Button.setImage(UIImage(named: "*"), for: .normal)
        Button.setImage(UIImage(named: "*"), for: .disabled)
        // 失效属性,影会响外观
        Button.isEnabled = false     
        // 此属性禁用按钮点击的同时不影响其外观
        Button.isUserInteractionEnabled = false

おすすめ

転載: blog.csdn.net/u012477117/article/details/121559259