swift - 如何使用3种颜色制作UIView渐变效果

标签 swift uiview gradient swift4.2

我也想使用中心颜色。有人可以帮我解决这个问题吗?

extension UIView {
    func setGradientBackground(topColor: UIColor, bottomColor: UIColor) {
        let gradientLayer       = CAGradientLayer()
        gradientLayer.frame     = bounds
        gradientLayer.colors    = [topColor.cgColor, bottomColor.cgColor]
        gradientLayer.locations = [0.0, 1.0]
        gradientLayer.startPoint    = CGPoint(x: 0.0, y: 0.0)
        gradientLayer.endPoint      = CGPoint(x: 0.0, y: 1.0)
        layer.insertSublayer(gradientLayer, at: 0)
    }
}

最佳答案

只需添加:

gradientLayer.colors = [topColor.cgColor, <centerColor.cgColor>,bottomColor.cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0] //you can remove this line if you want even gradient.

gradientLayer.colorsgradientLayer.locations 不限于每个仅两个输入。

此外,startPointendPoint 分别默认为 (0.5,0.0) 和 (0.5,1.0)。您可以在这里引用它的样子:

http://ios-tutorial.com/create-uiview-gradient-background/

关于swift - 如何使用3种颜色制作UIView渐变效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59377440/

相关文章:

ios - 如何将使用 Interface Builder 创建的 View 显示为 UIPopoverController?

ios - 如何将参数从一个类传递到 UIView 类? swift

css - Mozilla 渐变到其他渐变

ios - 如何给 SwiftUI arc 添加阴影?

ios - Swift 中的 Facebook iOS SDK v 4.1.0 和 Cocoapods : cannot import modules

iOS - swift - 为 secp224k1 曲线(ECDH)生成 key 对

swift - 在 UITabBar 上方显示键盘

ios - 将添加的行设置为第一个 indexPath 行

ios - UIPageViewController subview 在屏幕点击后移到状态栏后面

html - 我可以使用带边框颜色的渐变吗?