2023-11-13|閱讀時間 ‧ 約 23 分鐘

[iOS][swift]UIView顏色漸層


class GradientView: UIView {

private var gradientLayer = CAGradientLayer()
private var vertical: Bool = false

func draw(_ colors: [Any]? , isVertical : Bool, locations: [NSNumber]? = [0.0, 1.0]) {
self.vertical = isVertical

//fill view with gradient layer
gradientLayer.frame = self.bounds

//style and insert layer if not already inserted
if gradientLayer.superlayer == nil {

gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = vertical ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0)
gradientLayer.colors = colors//[UIColor.green.cgColor, UIColor.red.cgColor]
gradientLayer.locations = locations

self.layer.insertSublayer(gradientLayer, at: 0)
}
}

}




分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.