[iOS][swift]UIView顏色漸層

小黑
發佈於APP
2023/11/13閱讀時間約 2 分鐘


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)
}
}

}




7會員
78內容數
嗨,我是一名程式設計師,會在這分享開發與學習紀錄。
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容