iOS 渐变 Button

效果

iOS 渐变 Button_第1张图片
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)