Set Background Gradient on Button in Swift

前端 未结 10 1257
礼貌的吻别
礼貌的吻别 2020-12-12 21:54

I have no idea how to set the background gradient on a button (without making the background gradient an image). This is so different from Android.

Here\'s a class I

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 22:17

    There are already many answers there I want add what I did to achieve this. I use this custom Button GradientButton

    import Foundation
    import UIKit
    
    class GradientButton: UIButton {
    
        let gradientColors : [UIColor]
        let startPoint : CGPoint
        let endPoint : CGPoint
    
        required init(gradientColors: [UIColor] = [UIColor.red, UIColor.blue],
                      startPoint: CGPoint = CGPoint(x: 0, y: 0.5),
                      endPoint: CGPoint = CGPoint(x: 1, y: 0.5)) {
            self.gradientColors = gradientColors
            self.startPoint = startPoint
            self.endPoint = endPoint
    
            super.init(frame: .zero)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            let halfOfButtonHeight = layer.frame.height / 2
            contentEdgeInsets = UIEdgeInsets(top: 10, left: halfOfButtonHeight, bottom: 10, right: halfOfButtonHeight)
    
            layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    
            backgroundColor = UIColor.clear
    
            // setup gradient
    
            let gradient = CAGradientLayer()
            gradient.frame = bounds
            gradient.colors = gradientColors.map { $0.cgColor }
            gradient.startPoint = startPoint
            gradient.endPoint = endPoint
            gradient.cornerRadius = 4
    
            // replace gradient as needed
            if let oldGradient = layer.sublayers?[0] as? CAGradientLayer {
                layer.replaceSublayer(oldGradient, with: gradient)
            } else {
                layer.insertSublayer(gradient, below: nil)
            }
    
            // setup shadow
    
            layer.shadowColor = UIColor.darkGray.cgColor
            layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: halfOfButtonHeight).cgPath
            layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
            layer.shadowOpacity = 0.85
            layer.shadowRadius = 4.0
        }
    
        override var isHighlighted: Bool {
            didSet {
                let newOpacity : Float = isHighlighted ? 0.6 : 0.85
                let newRadius : CGFloat = isHighlighted ? 6.0 : 4.0
    
                let shadowOpacityAnimation = CABasicAnimation()
                shadowOpacityAnimation.keyPath = "shadowOpacity"
                shadowOpacityAnimation.fromValue = layer.shadowOpacity
                shadowOpacityAnimation.toValue = newOpacity
                shadowOpacityAnimation.duration = 0.1
    
                let shadowRadiusAnimation = CABasicAnimation()
                shadowRadiusAnimation.keyPath = "shadowRadius"
                shadowRadiusAnimation.fromValue = layer.shadowRadius
                shadowRadiusAnimation.toValue = newRadius
                shadowRadiusAnimation.duration = 0.1
    
                layer.add(shadowOpacityAnimation, forKey: "shadowOpacity")
                layer.add(shadowRadiusAnimation, forKey: "shadowRadius")
    
                layer.shadowOpacity = newOpacity
                layer.shadowRadius = newRadius
    
                let xScale : CGFloat = isHighlighted ? 1.025 : 1.0
                let yScale : CGFloat = isHighlighted ? 1.05 : 1.0
                UIView.animate(withDuration: 0.1) {
                    let transformation = CGAffineTransform(scaleX: xScale, y: yScale)
                    self.transform = transformation
                }
            }
        }
    }
    

    You can make GradientButton instance like this.

    let button = GradientButton.init(gradientColors:[UIColor.black, UIColor.white], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1))
    

提交回复
热议问题