iOS 渐变 Button

作者: xiAo__Ju | 来源:发表于2017-06-19 22:12 被阅读0次

效果

Simulator Screen Shot 2017年6月19日 下午10.07.14.png
class GradientButton: UIButton {
    override class var layerClass: AnyClass {
        return CAGradientLayer.self
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        if let gradientLayer = layer as? CAGradientLayer {
            gradientLayer.colors = [
                UIColor(r: 0, g: 233, b: 249).cgColor,
                UIColor(r: 50, g: 254, b: 217).cgColor,
            ]

            gradientLayer.locations = [0.25, 0.75]
            // set gradient start and end points
            gradientLayer.startPoint = CGPoint(x: 0, y: 0)
            gradientLayer.endPoint = CGPoint(x: 1, y: 0)

            layer.cornerRadius = frame.height / 2
            layer.shadowColor = UIColor(r: 0, g: 233, b: 249).cgColor
            layer.shadowOffset = CGSize(width: 0, height: 2)
            layer.shadowOpacity = 0.9
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Demo

相关文章

网友评论

    本文标题:iOS 渐变 Button

    本文链接:https://www.haomeiwen.com/subject/opmnqxtx.html