14

我正在尝试使用CaGradientLayer. 我知道可以使用startPointand定义角度endPoint。我可以为一些标准角度计算这些点,如 0、90、180、360 等。但我想为任意角度制定这些点。我曾尝试使用一些三角函数来计算它,但没有取得任何成功。谁能给我任何关于如何计算任意角度的这些点的指示?

4

3 回答 3

14

斯威夫特 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)
    
    
}
于 2017-07-05T10:00:01.463 回答
9

这在很大程度上基于 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).

于 2017-08-31T18:25:22.190 回答
8

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

在 0.0 时,渐变是水平的(颜色 A 在上面,颜色 B 在下面),旋转 360 度到值 1.0(与值 0.0 相同 - 或完全旋转)。

例如,当 x = 0.25 时,颜色 A 为左,颜色 B 为右。在 0.5 时,颜色 A 在下方,颜色 B 在上方,0.75 颜色 A 在右侧,颜色 B 在左侧。它从右到左逆时针旋转。

它有四个参数:frame、colorA、colorB 和输入值 (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;
}
于 2015-03-20T14:16:20.477 回答