【问题标题】:Setting up Gradient Background Colors in Interface Builder for a Gradient View在 Interface Builder 中为渐变视图设置渐变背景颜色
【发布时间】:2019-09-04 05:12:42
【问题描述】:

我在属性检查器中启用了渐变视图,但我无法修复颜色以显示更多颜色的过渡,并且还显示颜色从下到上而不是从左到右?右侧窗格中需要进行哪些修改?

【问题讨论】:

    标签: ios xcode colors gradient


    【解决方案1】:

    您希望 Start PointX 和 End PointX 为 0。然后将 Start PointY 设置为 0,End PointY 设置为 1。这将给出一个从顶部到底部的渐变,从顶部的顶部颜色和底部的底部颜色开始.

    【讨论】:

    • 太棒了,谢谢@rmaddy!我也只是想问一下如何从顶角(对角线)更改渐变?
    • 根据需要设置起点和终点的 X 和 Y 值。左上角的 X Y 为 0,右下角的 X Y 为 1。
    【解决方案2】:

    老实说,我不知道您可以通过情节提要添加渐变,但如果您通过代码执行此操作,您将有更多选择。比如更多的颜色、颜色位置等

        // Create a gradient layer
       let gradient: CAGradientLayer = CAGradientLayer()
        // Create an array of colours you can use, as many colours as you require
        gradient.colors = [.blue.cgColor, .red.cgColor, .orange.cgColor].cgColor
        // Chose the locations for your colors, 0.5 is center
        gradient.locations = [0.0, 0.5, 1.0]
        // Start and end points so this goes from y: 0.0 to y: 1.0 so top to bottom
        gradient.startPoint = CGPoint(x: 1.0, y: 0.0)
        gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
        // Set the frame
        gradient.frame = CGRect(x: 0.0, y: 0.0, width: yourView.frame.size.width, height: yourView.frame.size.height)
        // Add to view
        yourView.layer.insertSublayer(gradient, at: 0)
    

    【讨论】:

    • OP 想要两种颜色从上到下的渐变。这个答案显示了如何获得三种颜色的从左到右的渐变。
    • 更新了答案以适应 OP 的问题(即从左到右)。更多的颜色只是为了显示在使用代码时有更多的多功能性。
    • 感谢大家回来帮助大家@r
    猜你喜欢
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2012-03-15
    • 2018-11-25
    相关资源
    最近更新 更多