如何从UI添加渐变角度?

时间:2019-01-18 12:14:40

标签: ios swift cagradientlayer

我有这段代码可以从界面中修改颜色:

@IBDesignable
class GradientView: UIView {
    @IBInspectable var firstColor: UIColor = UIColor.clear{
        didSet{
            updateView()
        }
    }

    @IBInspectable var secondColor: UIColor = UIColor.clear{
        didSet{
            updateView()
        }
    }

    override class var layerClass: AnyClass{
        get{
            return CAGradientLayer.self
        }
    }

    func updateView(){
        let layer = self.layer as! CAGradientLayer
        layer.colors = [ firstColor.cgColor, secondColor.cgColor ]
        layer.locations = [ 0.5 ]
    }
}

如何在此处添加以修改角度?

1 个答案:

答案 0 :(得分:0)

您可以使用endPoint的属性startPointCAGradientLayer。像这样:

let x: Double! = angle / 360.0
let a = pow(sinf(Float(2.0 * .pi * ((x + 0.75) / 2.0))),2.0);
let b = pow(sinf(Float(2 * .pi * ((x+0.0)/2))),2);
let c = pow(sinf(Float(2 * .pi * ((x+0.25)/2))),2);
let d = pow(sinf(Float(2 * .pi * ((x+0.5)/2))),2);

我希望这会有所帮助!