Tôi đã cố gắng tạo ra một nền gradient xuyên tâm cơ bản, nhưng không thành công. Tôi quản lý để có được một gradient tuyến tính làm việc như thể hiện với các mã dưới đây, nhưng tôi không có ý tưởng làm thế nào để làm cho nó xuyên tâm với màu sắc khác nhau - như trong hình dưới đây. Mọi sự trợ giúp sẽ rất được trân trọng. :)Nền Gradient xuyên tâm trong Swift
override func viewDidLoad() {
super.viewDidLoad()
//A linear Gradient Consists of two colours: top colour and bottom colour
let topColor = UIColor(red: 15.0/255.0, green: 118.0/255.0, blue: 128.0/255.0, alpha: 1.0)
let bottomColor = UIColor(red: 84.0/255.0, green: 187.0/255.0, blue: 187.0/255.0, alpha: 1.0)
//Add the top and bottom colours to an array and setup the location of those two.
let gradientColors: [CGColor] = [topColor.CGColor, bottomColor.CGColor]
let gradientLocations: [CGFloat] = [0.0, 1.0]
//Create a Gradient CA layer
let gradientLayer: CAGradientLayer = CAGradientLayer()
gradientLayer.colors = gradientColors
gradientLayer.locations = gradientLocations
gradientLayer.frame = self.view.bounds
self.view.layer.insertSublayer(gradientLayer, atIndex: 0)
}
nếu bạn đang làm theo cách này cho mục đích học tập tốt nhưng nếu bạn đang sử dụng mục đích thiết kế, tôi khuyên bạn nên thiết kế nó trong sketch3 và sử dụng nó như UIImage –
http://stackoverflow.com/questions/16043534/circular này -cagradientlayer-mask có thể giúp bạn. – RayChen
@sriramhegde Nền cần thay đổi động. Một hình ảnh sẽ không giúp ích gì. Cảm ơn. – Gugulethu