使用CAGradientLayer定义渐变的角度

时间:2014-11-12 12:08:53

标签: ios objective-c gradient linear-gradients cagradientlayer

我正在尝试使用CaGradientLayer绘制角度渐变。我知道角度可以使用startPointendPoint来定义。我可以为一些标准角度计算这些点,如0,90,180,360等。但我希望将这些点制定为任意角度。我曾尝试用一些三角函数计算它,但没有取得任何成功。任何人都可以给我任何关于如何计算任意角度这些点的指示吗?

3 个答案:

答案 0 :(得分:10)

Swift 3

static func setGradient(view: UIView!,viewRadius: CGFloat!, color1: UIColor!, color2: UIColor!, angle: Double!, alphaValue: CGFloat!){
    let gradient = CAGradientLayer()

    gradient.frame =  CGRect(origin: CGPoint.zero, size: view.frame.size)

    gradient.colors = [color1.withAlphaComponent(alphaValue).cgColor, color2.withAlphaComponent(alphaValue).cgColor]
    let x: Double! = angle / 360.0
    let a = pow(sinf(Float(2.0 * M_PI * ((x + 0.75) / 2.0))),2.0);
    let b = pow(sinf(Float(2*M_PI*((x+0.0)/2))),2);
    let c = pow(sinf(Float(2*M_PI*((x+0.25)/2))),2);
    let d = pow(sinf(Float(2*M_PI*((x+0.5)/2))),2);

    gradient.endPoint = CGPoint(x: CGFloat(c),y: CGFloat(d))
    gradient.startPoint = CGPoint(x: CGFloat(a),y:CGFloat(b))

    view.roundCorners([.topLeft, .bottomLeft], radius: viewRadius)
    view.layer.insertSublayer(gradient, at: 0)


}

答案 1 :(得分:7)

这是一种创建视图的方法,该视图允许基于来自滑块(或任何东西)的输入360度旋转其双色渐变。传入的滑块值(" x"下面的变量)介于0.0和1.0之间。

在0.0处,梯度是水平的(顶部是颜色A,下面是颜色B),旋转360度到1.0(与0.0相同 - 或完全旋转)。

E.g。当x = 0.25时,左边是颜色A,颜色B是右边。在0.5时,颜色A在下面,颜色B在上面,0.75颜色A在右边,颜色B在左边。它从右向左逆时针旋转。

它需要四个参数:frame,colourA,colourB和输入值(0-1)。

-(UIView *)gradientViewWithFrame:(CGRect)frame colourA:(UIColor *)A colourB:(UIColor *)B rotation:(float)x {

//x is between 0 and 1, eg. from a slider, representing 0 - 360 degrees
//colour A starts on top, with colour B below
//rotations move anti-clockwise

//1. create the colour view
UIView * colourView = [UIView new];
colourView.frame = frame;

//2. create the gradient layer
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = colourView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[A CGColor], (id)[B CGColor], nil];
[colourView.layer insertSublayer:gradient atIndex:0];

//3. create coordinates
float a = pow(sinf((2*M_PI*((x+0.75)/2))),2);
float b = pow(sinf((2*M_PI*((x+0.0)/2))),2);
float c = pow(sinf((2*M_PI*((x+0.25)/2))),2);
float d = pow(sinf((2*M_PI*((x+0.5)/2))),2);

//4. set the gradient direction
[gradient setStartPoint:CGPointMake(a, b)];
[gradient setEndPoint:CGPointMake(c, d)];

return colourView;
}

答案 2 :(得分:3)

这主要基于Sarthak Sharma的解决方案。我做了UIView的扩展,我认为可读性和类型转换可以改进一点:

extension UIView {

    func setGradient(colors: [CGColor], angle: Float = 0) {
        let gradientLayerView: UIView = UIView(frame: CGRect(x:0, y: 0, width: bounds.width, height: bounds.height))
        let gradient: CAGradientLayer = CAGradientLayer()
        gradient.frame = gradientLayerView.bounds
        gradient.colors = colors

        let alpha: Float = angle / 360
        let startPointX = powf(
            sinf(2 * Float.pi * ((alpha + 0.75) / 2)),
            2
        )
        let startPointY = powf(
            sinf(2 * Float.pi * ((alpha + 0) / 2)),
            2
        )
        let endPointX = powf(
            sinf(2 * Float.pi * ((alpha + 0.25) / 2)),
            2
        )
        let endPointY = powf(
            sinf(2 * Float.pi * ((alpha + 0.5) / 2)),
            2
        )

        gradient.endPoint = CGPoint(x: CGFloat(endPointX),y: CGFloat(endPointY))
        gradient.startPoint = CGPoint(x: CGFloat(startPointX), y: CGFloat(startPointY))

        gradientLayerView.layer.insertSublayer(gradient, at: 0)
        layer.insertSublayer(gradientLayerView.layer, at: 0)
    }

}

将此添加到新的或相关的Swift文件中,您所要做的就是调用myView.setGradient(colors: gradientColorsArray)myView.setGradient(colors: gradientColorsArray, angle: 90)